sum of first 10^n primes A099824

Max A. maxale at gmail.com
Wed Oct 4 17:42:51 CEST 2006


On 10/4/06, cino hilliard <hillcino368 at hotmail.com> wrote:

> >>%o A099824 (PARI) sumprimes(n) = a=1;sum(x=1,n,a=nextprime(a+1))
> >
> >PARI/GP can do faster than that. One needs just to run gp with an
> >appropriate parameter -p, and then define sumprimes simply as
> >sumprimes(n) = sum(x=1,n,prime(x))
> >sumprimesmax(n) = sum(x=1,n,prime(x))
>
> Did you test sumprimes(n) = sum(x=1,n,prime(x)) for timing? Maybe you are
> using
> gp2c?

No, I did not. I believed that prime() behaved like an array (in which
case it would be much faster than nextprime()).

> (05:19:13) gp > sumprimesmax(10^5)
> time = 4,828 ms.
> %274 = 62260698721
> (05:19:23) gp > sumprimescino(10^5)
> time = 750 ms.
> %275 = 62260698721
> (05:19:42) gp >
>
> So using nextprime() is about 4828/750 = 6.44 times faster than using
> prime()

Oh, that's surprising. Could you also try the following program:

sumprimes2(n) = local(s);s=0;forprime(p=2,prime(n),s+=p);s

Thanks,
Max






More information about the SeqFan mailing list