The code below works, but when I try to run it using P17 the compiler throws an 
error about the `gcd` proc. Using P17 causes `gcd` to be called on the order of 
250,000 times, as `modpg = 510510`

Here's the code.
    
    
    # Create constant parameters for chosen PG at compile time
    proc genPGparameters(prime: int): (int, int, int, seq[int]) =
      echo("generating parameters for P", prime)
      let primes = [2, 3, 5, 7, 11, 13, 17, 19, 23]
      var modpg = 1;  var excluded_primescnt = 0
      for prm in primes:
        excluded_primescnt += 1; modpg *= prm; if prm == prime: break
      
      var pc = 3; var residues: seq[int]; residues = @[]
      while pc < modpg:
        if gcd(modpg, pc) == 1: residues.add(pc)
        pc += 2
      residues.add(modpg + 1)
      let rescnt = residues.len
      result = (modpg, rescnt, excluded_primescnt, residues)
    
    # Generate at compile time the parameters for P17
    const parameters = genPGparameters(17)
    
    

Here's the relevant compiler output.
    
    
    generating parameters for P17
    stack trace: (most recent call last)
    ssozp17x1d5bparnew128.nim(59)
    ssozp17x1d5bparnew128.nim(52) genPGparameters
    lib/pure/math.nim(439)   gcd
    lib/pure/math.nim(439, 15) Error: interpretation requires too many 
iterations
    ➜  nim
    
    

I can't see how `gcd` could care how many times it's being called, so it seems 
it must be the compiler not liking it being called past a certain number.

Whatever the reason, how do I get this code to finish compiling? 

Reply via email to