-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathproblem132.gw
61 lines (54 loc) · 912 Bytes
/
problem132.gw
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
fun int powermod(int a, int b, int n) {
var int p;
var int x;
2 => p;
while (p * 2 <= b) {
p * 2 => p;
}
1 => x;
while (true) {
if (b >= p) {
(x * a) % n => x;
b - p => b;
}
p / 2 => p;
if (p <= 0) {
break;
}
(x * x) % n => x;
}
return x;
}
fun bool isprime(int x) {
if (x < 2) {
return false;
}
var int i;
2 => i;
while (i < x) {
if ((x % i) <= 0) {
return false;
}
i + 1 => i;
}
return true;
}
fun bool primedivides(int n, int p) {
var int m;
powermod(10, n, 9 * p) => m;
return m <= 1 && m >= 1;
}
var int i;
var int sum;
var int remaining;
2 => i;
0 => sum;
40 => remaining;
while (remaining > 0) {
if (primedivides(1000000000, i) && isprime(i)) {
sum + i => sum;
remaining - 1 => remaining;
}
i + 1 => i;
}
<<< sum >>>;