PARI/GP Control Statements

Contents

break ({$$n$$})

Exit $$n$$ innermost enclosing loops.

for…

for ($$x$$ = $$a$$, $$b$$, $$seq$$)

Evaluate $$seq$$ for $$a \le x \le b$$.

forcomposite ($$x$$ = $$a$$, $$b$$, $$seq$$)

Evaluate $$seq$$ for composites $$a \le x \le b$$.

fordiv ($$n$$, $$x$$, $$seq$$)

Evaluate $$seq$$ for $$x$$ dividing $$n$$.

fordivfactored ($$n$$, $$x$$, $$seq$$)

Evaluate $$seq$$ for $$x = [d, factor(d)], d | n$$.

forfactored ($$x$$ = $$a$$, $$b$$, $$seq$$)

Evaluate $$seq$$ for $$x = [n, factor(n)]$$, $$a \le n \le b$$.

Evaluates $$seq$$, where the formal variable $$x$$ is [$$n$$, factor($$n$$)] and $$n$$ goes from $$a$$ to $$b$$; $$a$$ and $$b$$ must be integers. Nothing is done if $$a > b$$.

gp > B = 10^9; for (N = B, B+10^6, factor(N))
time = 2,844 ms.
gp > B = 10^9; forfactored (N = B, B+10^6, [n, fan] = N)
time = 704 ms.

To do: Why are some of the results in different forms like [1, matrix(0,2)], [2, Mat([2, 1])], or [6, [2, 1; 3, 1]]?

gp > forfactored (x = 1, 20, [n, fan] = x; print([n, fan]))
[1, matrix(0,2)]
[2, Mat([2, 1])]
[3, Mat([3, 1])]
[4, Mat([2, 2])]
[5, Mat([5, 1])]
[6, [2, 1; 3, 1]]
[7, Mat([7, 1])]
[8, Mat([2, 3])]
[9, Mat([3, 2])]
[10, [2, 1; 5, 1]]
[11, Mat([11, 1])]
[12, [2, 2; 3, 1]]
[13, Mat([13, 1])]
[14, [2, 1; 7, 1]]
[15, [3, 1; 5, 1]]
[16, Mat([2, 4])]
[17, Mat([17, 1])]
[18, [2, 1; 3, 2]]
[19, Mat([19, 1])]
[20, [2, 2; 5, 1]]
time = 16 ms.

forpart ($$p$$ = $$n$$, $$seq$$)

Loop over partitions of $$n$$.

forperm ($$s$$, $$p$$, $$seq$$)

Loop over permutations of $$s$$.

forprime ($$x$$ = $$a$$, $$b$$, $$seq$$)

Evaluate $$seq$$ for primes $$a \le x \le b$$.

forprimestep ($$x$$ = $$a$$, $$b$$, $$seq$$)

Evaluate $$seq$$ for primes $$\equiv a\space(mod\space q)$$.

forqfvec ($$v$$, $$q$$, $$b$$, $$seq$$)

Loop over vectors $$v$$, $$q(v) \le b$$, $$q > 0$$.

forsquarefree ($$x$$ = $$a$$, $$b$$, $$seq$$)

— : : : as above, $$n$$ squarefree

Evaluates $$seq$$, where the formal variable $$x$$ is [$$x$$, factor($$x$$)] and $$x$$ goes through squarefree integers from $$a$$ to $$b$$; $$a$$ and $$b$$ must be integers of the same sign. Nothing is done if $$a \ge b$$.

forstep ($$x$$ = $$a$$, $$b$$, $$s$$, $$seq$$)

Evaluate $$seq$$ for $$a \le x \le b$$ stepping $$s$$.

forsubgroup ($$h$$ = $$g$$)

Loop over $$h < g$$ finite abelian group.

forsubset ($$n$$, $$p$$, $$seq$$)

Loop over subsets of $$\lbrace1, …, n\rbrace$$.

forsubset ([$$n$$, $$k$$], $$p$$, $$seq$$)

Loop over $$k$$-subsets of $$\lbrace1, …, n\rbrace$$.

forvec ($$x$$ = $$v$$, $$seq$$)

Multivariable for, lex ordering.

if ($$a$$, {$$seq1$$}, {$$seq2$$})

If $$a ≠ 0$$, evaluate $$seq1$$, else $$seq2$$.

next ({$$n$$})

Start new iteration of $$n$$-th enclosing loop.

return ({$$n$$})

Return $$n$$ from current subroutine.

until ($$a$$, $$seq$$)

Evaluate $$seq$$ until $$a \ne 0$$.

while ($$a$$, $$seq$$)

While $$a \ne 0$$, evaluate $$seq$$.