0

Is there a way to nest for loops inside other for loops in Pari/GP (2.7.6) since the following error always appears:

***   at top-level: read("prog.txt")
  ***                 ^----------------
  *** read: sorry, embedded braces (in parser) is not yet implemented.

Code:

(12:14) gp > n = 12
%12 = 12
(12:14) gp > k = 10
%13 = 10
(12:14) gp > g = [1..10]
%14 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]


{
  forprime(p = 1, 16,
  rn = n%p
  rk = k%p
{
    for(i=1, #g,
        if( (rn*(g[i]+rk)+1)%p!=0,
        h = concat(h, g[i])
}
  g = h
}

Thanks for help. The question where I posted and got the code from got deleted, so I'm kind of stuck at this point.

J. Linne
  • 275
  • 4
  • 15

1 Answers1

0

Braces don't mean the same thing as in C# or many other programming languages. They do not start/end a block.

In PARI multiple statements are joined with a semi-colon and contained within the argument parenthesis of a function. Special functions include while, for, if etc.

For example:

 my(s=7);forprime(p=1, 7, s*=3; for(i=1, p, s+=p^2); s+=6); s

contains three statements inside the forprime loop, separated by semicolons and one of those is another loop.

Now braces {} on the other hand, just allow you to write programs that are more than one line long. Normally when an end of line is reached, PARI tries to interpret everything on that line. But it doesn't do that if between { and }, so you never need more than one pair.

In your example you need to remove the inner {} and add some semi-colons between statements.

Andrew
  • 873
  • 4
  • 10