GP code examples for generating sequences

A note on "print" versus "write" in GP: when generating a sequence in GP, you can use the write command to output to a file. This, however, is often slow, since it requires opening the file for every term of the sequence. A quicker method is to use the tee command (in any unix-like environment) and call gp like this:


gp | tee output.txt 

This will start gp, but all output will be echoed to the file output.txt. Then, you can do something like

for(i=1,10^6,if(isprime(i),print(i))) 

to spit out all primes under 106 to the screen. Then quit gp, and open output.txt in a text editor and you will see all the primes there. Remove any extraneous lines, save, and you'll be ready to feed it to the score-from-list code.

Here is some GP code which generates the sequence of integers whose digits sum is 12 ( A235151).


for(i=1,10^6,if(sumdigits(i)==12,print(i)))

Here is some GP code which generates the sequence of abundant numbers (A005101).

for(i=1,10^6,if(sigma(i)>2*i,print(i)))

Here is some GP code whcih generates the Sophie Germain primes (A005384). The 78498 values is arrived at by noting that the 78498th prime is the largest one less than 106.

for(i=1,78498,if(isprime(2*prime(i)+1),print(prime(i))))