Skip to content

Commit 8b1fbf6

Browse files
authored
Merge branch 'TheRealDeath:main' into main
2 parents 0d0bf08 + 5a84215 commit 8b1fbf6

28 files changed

+145
-2
lines changed

.breakpoints

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
{
55
"id": "008e99a4-cee4-4ff3-8095-6f1c04512119",
66
"line": 1,
7-
"version": 539,
7+
"version": 540,
88
"index": 0
99
}
1010
]
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#include <iostream>
2+
using namespace std;
3+
4+
long long binpow(long long a, long long b)
5+
{
6+
long long res = 1;
7+
while (b > 0)
8+
{
9+
if (b & 1)
10+
res = res * a;
11+
a = a * a;
12+
b >>= 1;
13+
}
14+
return res;
15+
}
16+
17+
long long modulo_binpow(long long a, long long b, long long m)
18+
{
19+
a %= m;
20+
long long res = 1;
21+
while (b > 0)
22+
{
23+
if (b & 1)
24+
res = res * a % m;
25+
a = a * a % m;
26+
b >>= 1;
27+
}
28+
return res;
29+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#include <math.h>
2+
#include <stdio.h>
3+
#include <stdlib.h>
4+
5+
bool millerTest(int n, int d)
6+
{
7+
// 2.1 Pick a random number 'a' in range [2, n-2]
8+
int a = 2 + std::rand() % (n - 4);
9+
10+
// 2.2 Find: x = pow(a, d) % n
11+
int x = std::pow(a, d) % n;
12+
13+
// 2.3 x == 1 or x == n-1, return true.
14+
if (x == 1 || x == n - 1) return true;
15+
16+
// 2.4 While d does not become n-1:
17+
while (d != n - 1)
18+
{
19+
x = (x*x) % n;
20+
if (x == 1) return false;
21+
if (x == n-1) return true;
22+
}
23+
}
24+
25+
bool isPrime(int n, int k)
26+
{
27+
// 1.1 Handle base cases for n < 3
28+
if (n <= 1 || n == 4) return false;
29+
if (n <= 3) return true;
30+
31+
// 1.2 If n is even, return false.
32+
if (n % 2 == 0) return false;
33+
34+
/* 1.3
35+
Find an odd number d such that n-1 can be written as d*(2^r).
36+
Note that since n is odd, (n-1) must be even and r must be greater than 0.
37+
*/
38+
39+
int d = n - 1;
40+
while (d % 2 == 0)
41+
d /= 2;
42+
43+
// 1.4 Use the millerTest k times.
44+
for (int i = 0; i < k; ++i)
45+
{
46+
if (millerTest(n, d) == false)
47+
return false;
48+
}
49+
50+
// 1.5 If millerTest() does not return false, then the number is prime.
51+
return true;
52+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// Solovay-Strassen Primality Test
2+
#include <math.h>
3+
#include <stdio.h>
4+
#include <stdlib.h>
5+
#include <algorithm>
6+
#include "binary_exponentiation.cpp"
7+
8+
// No comments for code. Do not use, unless you understand it.
9+
int calculateJacobian(long long a, long long n)
10+
{
11+
if (!a) return 0;
12+
int ans = 1;
13+
if a < 0
14+
{
15+
a = -a;
16+
if (n % 4 == 3)
17+
ans = -ans;
18+
}
19+
if (a == 1) return ans;
20+
while (a)
21+
{
22+
if (a < 0)
23+
{
24+
a = -a;
25+
if (n % 4 == 3)
26+
ans = -ans;
27+
}
28+
while (a % 2 == 0)
29+
{
30+
a = a / 2;
31+
if (n % 8 == 3 || n % 8 == 5)
32+
ans = -ans;
33+
}
34+
35+
swap(a, n);
36+
37+
if (a % 4 == 3 && n % 4 == 3)
38+
ans = -ans;
39+
if (a > n / 2)
40+
a = a - n;
41+
}
42+
43+
if (n == 1) return ans;
44+
return 0;
45+
46+
}
47+
48+
bool solovayStrassen(long long p, int iterations)
49+
{
50+
if (p < 2) return false;
51+
if (p != 2 && p % 2 == 0) return false;
52+
53+
for (int i = 0; i < iterations; i++)
54+
{
55+
long long a = 1 + rand() % (p - 1);
56+
long long jacobian = (p + calculateJacobian(a, p)) % p;
57+
long long mod = modulo_binpow(a, (p - 1) / 2, p);
58+
59+
if (!jacobian || mod != jacobian) return false;
60+
}
61+
return true;
62+
}
File renamed without changes.

0 commit comments

Comments
 (0)