-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmath-optimization.cpp
More file actions
68 lines (53 loc) · 1.53 KB
/
math-optimization.cpp
File metadata and controls
68 lines (53 loc) · 1.53 KB
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
/**
* License:
* This Source Code Form is subject to the terms of
* the Mozilla Public License, v. 2.0. If a copy of
* the MPL was not distributed with this file, You
* can obtain one at http://mozilla.org/MPL/2.0/.
*
* Authors:
* David Ellsworth <davide.by.zero@gmail.com>
*/
#include "regex.h"
#include "math-optimization.h"
#ifdef USE_GMP
#pragma comment(lib, "libgmp-10.lib")
static int isPrime_is_initialized = false;
static mpz_t mpzN;
void init_isPrime()
{
if (isPrime_is_initialized)
return;
mpz_init(mpzN);
#if GMP_LIMB_BITS == 64
mpzN->_mp_size = 1;
#endif
isPrime_is_initialized = true;
}
int isPrime(Uint64 n)
{
mpz_set_uint64(mpzN, n);
return mpz_probab_prime_p(mpzN, 12); // 12 is enough for < 2^64, according to http://www.trnicely.net/misc/mpzspsp.html and to Jiang and Deng (2014) (doi:10.1090/S0025-5718-2014-02830-5)
}
#else
#include <math.h>
void init_isPrime()
{
}
// from https://stackoverflow.com/a/31758727/161468
int isPrime(Uint64 n)
{
if (n<=3 || n==5)
return n>1;
if (n%2==0 || n%3==0 || n%5==0)
return false;
if (n<=30)
return n==7 || n==11 || n==13 || n==17 || n==19 || n==23 || n==29;
Uint64 sq = (Uint64)ceil(sqrt((double)n));
for (Uint64 i=7; i<=sq; i+=30)
if (n%(i )==0 || n%(i+ 4)==0 || n%(i+ 6)==0 || n%(i+10)==0 ||
n%(i+12)==0 || n%(i+16)==0 || n%(i+22)==0 || n%(i+24)==0)
return false;
return true;
}
#endif