-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalgorithms.c
More file actions
62 lines (55 loc) · 1019 Bytes
/
algorithms.c
File metadata and controls
62 lines (55 loc) · 1019 Bytes
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
#include "algorithms.h"
#include <stdio.h>
bigint gcd(bigint a, bigint b) {
if (b > a) {
// swap
bigint tmp = a;
a = b;
b = tmp;
}
if (b == 0) {
return a;
}
bigint r = a % b;
while (r > 0) {
a = b;
b = r;
r = a % b;
}
return b;
}
bigint trial_division(bigint n) {
if (n % 2 == 0) {
return 2;
}
bigint limit = floor(sqrt(n));
for (bigint i = 3; i <= limit; i+=2) {
if (n % i == 0) {
return i;
}
}
return 1;
}
static bigint g(bigint x, bigint n) {
return ((x * x) + 1) % n;
}
bigint pollard_rho(bigint n) {
bigint x = 2;
bigint y = 2;
bigint d = 1;
bigint tmp;
while (d == 1) {
x = ((x * x) + 1) % n;;
y = g(((y * y) + 1) % n, n);
if (y > x) {
tmp = y - x;
} else {
tmp = x - y;
}
d = gcd(tmp, n);
}
if (d == n) {
return 1;
}
return d;
}