-
Notifications
You must be signed in to change notification settings - Fork 0
/
p46.cpp~
104 lines (95 loc) · 2.06 KB
/
p46.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
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
#include <iostream>
#include <stdio.h>
<<<<<<< HEAD
#include <stdlib.h>
using namespace std;
int solve()
{
=======
#include <math.h>
#include <stdlib.h>
using namespace std;
typedef signed long long int int64;
bool isPerfectSquare(int64 n)
{
if (n < 0)
return false;
int64 test = (int64)(sqrt(n)+0.5);
return test*test == n;
}
/*
Input: n > 3, an odd integer to be tested for primality;
Input: k, a parameter that determines the accuracy of the test
Output: composite if n is composite, otherwise probably prime
write n . 1 as 2sddddddd with d odd by factoring powers of 2 from n . 1
LOOP: repeat k times:
pick a random integer a in the range [2, n . 2]
x . ad mod n
if x = 1 or x = n . 1 then do next LOOP
for r = 1 .. s . 1
x . x2 mod n
if x = 1 then return composite
if x = n . 1 then do next LOOP
return composite
return probably prime
*/
bool isPrime(long n)
{
if (n == 2)
return true;
if (n < 3)
return false;
int k = 20;
unsigned long s;
unsigned long d = n-1;
while (d % 2 == 0)
{
s++;
d /= 2;
}
for (int i = 0; i < k; i++)
{
unsigned long a = rand() % (n-2);
if (a < 2)
a = 2;
unsigned long x = (unsigned long)((unsigned long)pow(a,d) % n);
if (x == 1 || x == n-1)
continue;
for (unsigned long r = 1; r < s; r++)
{
x = (x*x) % n;
if (x == 1)
return false;
if (x == n-1)
goto LOOP;
}
return false;
LOOP:
3;
}
return true;
}
int solve()
{
int i = 1;
bool found = false;
while(!found)
{
if (3)
i += 2;
}
>>>>>>> 51e27a5beb9ebe621e13137284a029ccebb8e06a
}
int main()
{
<<<<<<< HEAD
solve();
=======
for(int i = 0; i < 100; i++)
{
if (isPrime(i))
cout << i << endl;
}
>>>>>>> 51e27a5beb9ebe621e13137284a029ccebb8e06a
return 0;
}