-
Notifications
You must be signed in to change notification settings - Fork 7
/
test-pi.cpp
79 lines (68 loc) · 1.91 KB
/
test-pi.cpp
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
/*
* The Miller-Rabin primality test
*
* Written by Christian Stigen Larsen, 2012-01-10
* http://csl.sublevel3.org
*
* Distributed under the modified BSD license
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <time.h>
#include "miller-rabin.h"
/*
* Return the number of primes less than or equal to n, by virtue of brute
* force. There are much faster ways of computing this number, but we'll
* use it to test the primality function.
*
*/
static int pi(int n)
{
int r=0, m=2;
while ( m < n )
if ( isprime(m++) ) ++r;
return r;
}
/*
* Demonstration on how to use other PRNG functions than rand().
*/
static uint64_t randcalls = 0;
int myrand()
{
++randcalls;
return rand();
}
int main()
{
/*
* Instead of honoring my own advice over at
* http://csl.sublevel3.org/c++/#srand_time we'll just go ahead and use
* the idiomatic form for initializing the seed. (It doesn't really matter
* in this code).
*/
srand(time(0));
setrand(myrand, RAND_MAX);
printf(
"Calculating pi(n) by using the Miller-Rabin primality test.\n"
"\n"
"While this is a SLOW way of computing pi(n), we use it to test\n"
"the accuracy parameter `k´.\n"
"\n"
"Note that since this is a probabilistic algorithm, each run can\n"
"produce different results. That is why you might see incorrect\n"
"results below, from time to time.\n"
"\n"
"Written by Christian Stigen Larsen, http://csl.sublevel3.org\n"
"\n"
"For this run, k = %d\n\n", DEFAULT_ACCURACY);
int expected[] = {0, 4, 25, 168, 1229, 9592, 78498, 664579};
for ( int n=1, e=0; n<=10000000; n*=10, ++e ) {
int primes = pi(n);
printf("There are %d primes less than %d", primes, n);
if ( primes == expected[e] ) printf("\n");
else printf(" --- FAIL, expecteded %d\n", expected[e]);
}
printf("\nThe randomization function was called %Lu times\n", randcalls);
return 0;
}