primorial(negative)

Marco Bodrato bodrato at mail.dm.unipi.it
Fri Nov 13 19:34:36 UTC 2015


Ciao!

Il Ven, 13 Novembre 2015 8:07 pm, Torbjörn Granlund ha scritto:
> Here is some new code:

Is the list removing attachments? Or we both forgot to send the file?

By the way, mine is short enough to be sent inline :-)

Best regards,
m

------8<---------8<----------

/* gmp_numberofprimesupto_ui (N) -- Returns the number the primes in
   the range [0..N].

THE FUNCTION IN THIS FILE USE INTERNAL FUNCTIONS OF GMP, WITH A
MUTABLE INTERFACE.  IT WAS ONLY TESTED WITH GMP-6.1; IT IS ALMOST
GUARANTEED THAT IT WILL NOT WORK WITH A FUTURE GNU MP RELEASE.

Copyright 2012, 2015 Marco Bodrato.

The main() function inherits portions of the utils_getprime.c file by
Paul Zimmermann and Alexander Kruppa.

This file is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation; either version 3 of the License, or (at your
option) any later version.

see https://www.gnu.org/licenses/.  */

#include "gmp.h"
#include "gmp-impl.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/*********************************************************/
/* Section sieve: sieving functions and tools for primes */
/*********************************************************/

static mp_limb_t
n_to_bit (mp_limb_t n) { return ((n-5)|1)/3U; }

static mp_size_t
primesieve_size (mp_limb_t n) { return n_to_bit(n) / GMP_LIMB_BITS + 1; }

unsigned long
gmp_numberofprimesupto_ui (unsigned long n)
{
  static const unsigned table[] = { 0, 0, 1, 2, 2 };

  if (n < numberof (table))
    return table[n];
  else
    {
      mp_limb_t *sieve;
      mp_size_t size;
      unsigned int ret;

      size = primesieve_size (n);

      sieve = __GMP_ALLOCATE_FUNC_LIMBS (size);
      ret = gmp_primesieve (sieve, n);
      __GMP_FREE_FUNC_LIMBS (sieve, size);

      return table[numberof (table)-1] + ret;
    }
}

#ifdef MAIN
int
main (int argc, char *argv[])
{
  unsigned long N;

  N = strtoul (argv[1], NULL, 0);

  printf ("pi(%lu)=%lu\n", N, gmp_numberofprimesupto_ui (N));

  return 0;
}
#endif




More information about the gmp-discuss mailing list