(root)/
gmp-6.3.0/
mpz/
primorial_ui.c
       1  /* mpz_primorial_ui(RES, N) -- Set RES to N# the product of primes <= N.
       2  
       3  Contributed to the GNU project by Marco Bodrato.
       4  
       5  Copyright 2012, 2015, 2016, 2021 Free Software Foundation, Inc.
       6  
       7  This file is part of the GNU MP Library.
       8  
       9  The GNU MP Library is free software; you can redistribute it and/or modify
      10  it under the terms of either:
      11  
      12    * the GNU Lesser General Public License as published by the Free
      13      Software Foundation; either version 3 of the License, or (at your
      14      option) any later version.
      15  
      16  or
      17  
      18    * the GNU General Public License as published by the Free Software
      19      Foundation; either version 2 of the License, or (at your option) any
      20      later version.
      21  
      22  or both in parallel, as here.
      23  
      24  The GNU MP Library is distributed in the hope that it will be useful, but
      25  WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
      26  or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
      27  for more details.
      28  
      29  You should have received copies of the GNU General Public License and the
      30  GNU Lesser General Public License along with the GNU MP Library.  If not,
      31  see https://www.gnu.org/licenses/.  */
      32  
      33  #include "gmp-impl.h"
      34  
      35  /* TODO: Remove duplicated constants / macros / static functions...
      36   */
      37  
      38  /*************************************************************/
      39  /* Section macros: common macros, for swing/fac/bin (&sieve) */
      40  /*************************************************************/
      41  
      42  #define FACTOR_LIST_STORE(P, PR, MAX_PR, VEC, I)		\
      43    do {								\
      44      if ((PR) > (MAX_PR)) {					\
      45        (VEC)[(I)++] = (PR);					\
      46        (PR) = (P);						\
      47      } else							\
      48        (PR) *= (P);						\
      49    } while (0)
      50  
      51  /*********************************************************/
      52  /* Section sieve: sieving functions and tools for primes */
      53  /*********************************************************/
      54  
      55  #if WANT_ASSERT
      56  /* n_to_bit (n) = ((n-1)&(-CNST_LIMB(2)))/3U-1 */
      57  static mp_limb_t
      58  n_to_bit (mp_limb_t n) { return ((n-5)|1)/3U; }
      59  
      60  static mp_size_t
      61  primesieve_size (mp_limb_t n) { return n_to_bit(n) / GMP_LIMB_BITS + 1; }
      62  #endif
      63  
      64  /*********************************************************/
      65  /* Section primorial: implementation                     */
      66  /*********************************************************/
      67  
      68  void
      69  mpz_primorial_ui (mpz_ptr res, unsigned long n)
      70  {
      71    ASSERT (n <= GMP_NUMB_MAX);
      72  
      73    if (n < 5)
      74      {
      75        /* The smallest 5 results for primorial are stored */
      76        /* in a 15-bits constant (five octal digits)	 */
      77        MPZ_NEWALLOC (res, 1)[0] = (066211 >> (n * 3)) & 7;
      78        SIZ (res) = 1;
      79      }
      80    else
      81      {
      82        mp_limb_t *sieve, *factors;
      83        mp_size_t size, j;
      84        mp_limb_t prod;
      85        TMP_DECL;
      86  
      87        /* Try to estimate the result size, to avoid	*/
      88        /* resizing, and to initially store the sieve.	*/
      89        size = n / GMP_NUMB_BITS;
      90        size = size + (size >> 1) + 1;
      91        ASSERT (size >= primesieve_size (n));
      92        sieve = MPZ_NEWALLOC (res, size);
      93        size = (gmp_primesieve (sieve, n) + 1) / log_n_max (n) + 1;
      94  
      95        TMP_MARK;
      96        factors = TMP_ALLOC_LIMBS (size);
      97  
      98        j = 0;
      99  
     100        prod = 6;
     101  
     102        /* Store primes from 5 to n */
     103        {
     104  	mp_limb_t max_prod;
     105  
     106  	max_prod = GMP_NUMB_MAX / n;
     107  
     108  	/* Loop on sieved primes. */
     109  	for (mp_limb_t i = 4, *sp = sieve; i < n; i += GMP_LIMB_BITS * 3)
     110  	  for (mp_limb_t b = i, x = ~ *(sp++); x != 0; b += 3, x >>= 1)
     111  	    if (x & 1)
     112  	      {
     113  		mp_limb_t prime = b | 1;
     114  		FACTOR_LIST_STORE (prime, prod, max_prod, factors, j);
     115  	      }
     116        }
     117  
     118        if (j != 0)
     119  	{
     120  	  factors[j++] = prod;
     121  	  mpz_prodlimbs (res, factors, j);
     122  	}
     123        else
     124  	{
     125  	  PTR (res)[0] = prod;
     126  	  SIZ (res) = 1;
     127  	}
     128  
     129        TMP_FREE;
     130      }
     131  }