-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathproblem111.n
147 lines (135 loc) · 3.55 KB
/
problem111.n
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
using System;
using System.Numerics;
def PowMod(a : BigInteger, b : BigInteger, n : BigInteger) : BigInteger {
if (b == 0) {
1;
} else if (b % 2 == 0) {
def x = PowMod(a, b / 2, n);
(x * x) % n;
} else {
(PowMod(a, b - 1, n) * a) % n;
}
}
def Factorial(n : BigInteger) : BigInteger {
mutable result : BigInteger = 1;
for (mutable i = 1; i <= n; i++) {
result *= i;
}
result;
}
def nCr(n : BigInteger, r : BigInteger) : BigInteger {
Factorial(n) / (Factorial(r) * Factorial(n - r));
}
def MillerRabinTest(n : BigInteger, d : BigInteger, r : BigInteger, a : BigInteger) : bool {
mutable x = PowMod(a, d, n);
if ((x == 1) || (x == n - 1)) {
true;
} else {
break: {
for (mutable i = 0; i < r; i++) {
x = (x * x) % n;
when (x == n - 1)
break(true);
}
false;
}
}
}
def IsPrime(n: BigInteger) : bool {
if (n < 2) {
false;
} else if ((n == 2) || (n == 3) || (n == 5) || (n == 13) || (n == 23) || (n == 1662803)) {
true;
} else if ((n % 2 == 0) || (n % 3 == 0) || (n % 5 == 0)) {
false;
} else {
mutable d = n - 1;
mutable r = 0;
while (d % 2 == 0) {
d /= 2;
r += 1;
}
if (!MillerRabinTest(n, d, r, 2)) {
false;
} else if (!MillerRabinTest(n, d, r, 13)) {
false;
} else if (!MillerRabinTest(n, d, r, 23)) {
false;
} else if (!MillerRabinTest(n, d, r, 1662803)) {
false;
} else {
true;
}
}
}
def ToLong(data : array[Int32]) : BigInteger {
mutable result : BigInteger = 0;
foreach (x in data) {
result = 10 * result + x;
}
result;
}
def Generate(vec : array[BigInteger], digits : Int32, repeated : Int32, count : Int32, data : array[Int32], index : Int32, resultPosArg : Int32) : Int32 {
mutable resultPos = resultPosArg;
if (count < 0) {
// Return
resultPos;
} else if (index >= digits) {
vec[resultPos] = ToLong(data);
resultPos + 1;
} else if (digits - index < count) {
// Return
resultPos;
} else if (digits - index == count) {
for (mutable i = index; i < digits; i++) {
data[i] = repeated;
}
Generate(vec, digits, repeated, 0, data, digits, resultPos);
} else {
for (mutable i = 0; i < 10; i++) {
when ((i != 0) || (index != 0)) {
data[index] = i;
def newCount0 = if(repeated == i) count - 1 else count;
resultPos = Generate(vec, digits, repeated, newCount0, data, index + 1, resultPos);
}
}
resultPos;
}
}
def GenerateWithDigits(digits : Int32, repeated : Int32, count : Int32) : array[BigInteger] {
def resultArraySize = (BigInteger.Pow(10, digits - count) * nCr(digits, count)) :> Int32;
def result : array[BigInteger] = array(resultArraySize);
def data = array(digits);
def _ = Generate(result, digits, repeated, count, data, 0, 0);
result;
}
def MaxDigits(digits : Int32, repeated : Int32) : Int32 {
return: {
for (mutable i = digits; i >= 0; i--) {
def nums = GenerateWithDigits(digits, repeated, i);
foreach (num in nums) {
when (IsPrime(num)) {
return(i);
}
}
}
0;
}
}
def SumAll(digits : Int32, repeated : Int32, count : Int32) : BigInteger {
mutable total : BigInteger = 0;
def nums = GenerateWithDigits(digits, repeated, count);
foreach (num in nums) {
when (IsPrime(num)) {
total += num;
}
}
total;
}
def digits = 10;
mutable sumTotal: BigInteger = 0;
for (mutable i = 0; i < 10; i++) {
def count = MaxDigits(digits, i);
sumTotal += SumAll(digits, i, count);
}
System.Console.WriteLine(sumTotal);