primorial(negative)

paul zimmermann Paul.Zimmermann at inria.fr
Fri Nov 13 08:12:36 UTC 2015


       Hi Marco,

> Date: Fri, 13 Nov 2015 08:11:56 +0100
> From: "Marco Bodrato" <bodrato at mail.dm.unipi.it>
> 
> Ciao,
> 
> Il Gio, 12 Novembre 2015 10:10 am, paul zimmermann ha scritto:
> > mpz_prime_t p;
> 
> mpz_t p;
> 
> > mpz_prime_init_ui (p, 17); /* initializes p to 17 */
> 
> mpz_init_set_ui (p, 17);
> 
> > mpz_prime_next (p);     /* p <- next_prime(p) */
> 
> mpz_nextprime (p, p);
> 
> > mpz_prime_clear (p);
> 
> mpz_clear (p);
> 
> Sorry Paul, but I'm not able to get the point of the interface you proposed.
> 
> Best regards,
> m

efficiency: the GMP program below takes 1.5 seconds on my computer
to loop over all primes up to 10^6:

zimmerma at tarte:/tmp$ gcc -O2 -g e.c -lgmp
zimmerma at tarte:/tmp$ time ./a.out 1000000
pi(1000000) = 78498

real    0m1.510s
user    0m1.500s
sys     0m0.000s

while Sage takes only 0.05 second:

┌────────────────────────────────────────────────────────────────────┐
│ SageMath Version 6.8, Release Date: 2015-07-26                     │
│ Type "notebook()" for the browser-based notebook interface.        │
│ Type "help()" for help.                                            │
└────────────────────────────────────────────────────────────────────┘
sage: t = cputime()
sage: s = 0
sage: for p in prime_range(10^6):
....:     s += 1
....: print s, cputime(t)
....: 
78498 0.052

Paul

#include <stdio.h>
#include <stdlib.h>
#include "gmp.h"

int
main (int argc, char *argv[])
{
  int X = atoi (argv[1]);
  mpz_t p;
  int pi = 0;

  mpz_init_set_ui (p, 2);
  while (mpz_cmp_ui (p, X) < 0)
    {
      pi ++;
      mpz_nextprime (p, p);
    }
  printf ("pi(%d) = %d\n", X, pi);
  mpz_clear (p);
}


More information about the gmp-discuss mailing list