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