-
Notifications
You must be signed in to change notification settings - Fork 0
/
NumberTheory.java
288 lines (260 loc) · 5.54 KB
/
NumberTheory.java
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
import java.util.*;
public class NumberTheory {
static ArrayList<Integer> temp = new ArrayList<Integer>();
static ArrayList<Long> tempLong = new ArrayList<Long>();
public static boolean[] isPrimeArray(int n) {
boolean[] isPrime = new boolean[n + 1];
for (int i = 2; i <= n; i++) {
isPrime[i] = true;
}
for (int i = 2, j; (j = i * i) <= n; i++) {
if (!isPrime[i]) {
continue;
}
do {
isPrime[j] = false;
j += i;
} while (j <= n);
}
return isPrime;
}
public static int[] primesUpTo(int n) {
boolean[] isPrime = isPrimeArray(n);
int m = 0;
for (int i = 2; i <= n; i++) {
if (isPrime[i]) {
m++;
}
}
int[] primes = new int[m];
m = 0;
for (int i = 2; i <= n; i++) {
if (isPrime[i]) {
primes[m++] = i;
}
}
return primes;
}
public static int[] firstPrimes(int amount) {
int m = amount;
while (true) {
int[] a = primesUpTo(m);
if (a.length >= amount) {
return Arrays.copyOf(a, amount);
}
m *= 2;
}
}
/**
* 24 -> [2, 2, 2, 3]
*/
public static int[] primeFactorization(int n) {
temp.clear();
for (int i = 2; n > 1 && i * i <= n; i++) {
while (n % i == 0) {
temp.add(i);
n /= i;
}
}
if (n > 1)
temp.add(n);
int[] factors = new int[temp.size()];
for (int i = 0; i < factors.length; i++) {
factors[i] = temp.get(i);
}
return factors;
}
public static long[] primeFactorization(long n) {
tempLong.clear();
for (long i = 2; n > 1 && i * i <= n; i++) {
while (n % i == 0) {
tempLong.add(i);
n /= i;
}
}
if (n > 1)
tempLong.add(n);
long[] factors = new long[tempLong.size()];
for (int i = 0; i < factors.length; i++) {
factors[i] = tempLong.get(i);
}
return factors;
}
/**
* 24 -> [1, 2, 3, 4, 6, 8, 12, 24]
* TODO improve
*/
public static int[] divisors(int n) {
int i;
temp.clear();
for (i = 1; i * i < n; i++) {
if (n % i == 0)
temp.add(i);
}
int[] divisors;
if (i * i == n) {
divisors = new int[2 * temp.size() + 1];
divisors[temp.size()] = i;
} else
divisors = new int[2 * temp.size()];
for (int j = 0; j < temp.size(); j++) {
divisors[j] = temp.get(j);
divisors[divisors.length - 1 - j] = n / temp.get(j);
}
return divisors;
}
public static int gcd(int a, int b) {
while (a > 0) {
int t = b % a;
b = a;
a = t;
}
return b;
}
public static long gcd(long a, long b) {
while (a > 0) {
long t = b % a;
b = a;
a = t;
}
return b;
}
public static int gcdExtended(int a, int b, int[] xy) {
if (a == 0) {
xy[0] = 0;
xy[1] = 1;
return b;
}
int d = gcdExtended(b % a, a, xy);
int t = xy[0];
xy[0] = xy[1] - (b / a) * xy[0];
xy[1] = t;
return d;
}
public static int modInverse(int x, int p) {
int[] xy = new int[2];
int gcd = gcdExtended(x, p, xy);
if (gcd != 1) {
throw new IllegalArgumentException(x + ", " + p);
}
int result = xy[0] % p;
if (result < 0) {
result += p;
}
return result;
}
public static long lcm(long a, long b) {
return (a / gcd(a, b)) * b;
}
public static int[] fact, factInv;
public static void precalcFactorials(int n, final int M) {
fact = new int[n];
factInv = new int[fact.length];
fact[0] = factInv[0] = 1;
for (int i = 1; i < fact.length; i++) {
fact[i] = (int) (1L * fact[i - 1] * i % M);
factInv[i] = modInverse(fact[i], M);
}
}
public static int a(int n, int k, final int M) {
return (int) (1L * fact[n] * factInv[n - k] % M);
}
public static int c(int n, int k, final int M) {
return (int) (1L * a(n, k, M) * factInv[k] % M);
}
public static boolean isPrime1(int n) {
for (int i = 2; i * i <= n; i++) {
if (n % i == 0)
return false;
}
return n >= 2;
}
static final int[] toCheck = new int[]{2, 3, 5, 7};
public static boolean isPrime2(int n) {
if ((n & 1) == 0)
return n == 2;
if (n < 8)
return n > 1;
int d = n - 1;
int s = 0;
while ((d & 1) == 0) {
d >>= 1;
s++;
}
aloop:
for (int a : toCheck) {
if (n % a == 0)
return false;
long p = power(a, d, n);
if (p != 1) {
for (int r = 0; r < s; r++) {
if (p == n - 1)
continue aloop;
p *= p;
p %= n;
}
return false;
}
}
return true;
}
public static long power(long base, long p, long modulo) {
if (p < 0) {
throw new IllegalArgumentException("" + p);
}
if (p == 0) {
return (modulo == 1) ? 0 : 1;
}
long v = power(base, p / 2, modulo);
v = (v * v) % modulo;
return (p & 1) == 0 ? v : (v * base) % modulo;
}
public static final int mix(int x) {
x ^= x >>> 16;
x *= 0x85ebca6b;
x ^= x >>> 13;
x *= 0xc2b2ae35;
x ^= x >>> 16;
return x;
}
public static final long mix(long x) {
x ^= x >>> 33;
x *= 0xff51afd7ed558ccdL;
x ^= x >>> 33;
x *= 0xc4ceb9fe1a85ec53L;
x ^= x >>> 33;
return x;
}
static final int M = 1_000_000_007;
static int add(int a, int b) {
return (a + b) % M;
}
static int mul(int a, int b) {
return (int) ((a * (long) b) % M);
}
static int div(int a, int b) {
return mul(a, modInverse(b, M));
}
static int pow(int base, int p) {
if (p < 0) {
return pow(modInverse(base, M), -p);
}
if (p == 0) {
return 1;
}
int v = pow(base, p / 2);
v = mul(v, v);
return (p & 1) == 0 ? v : mul(v, base);
}
public static void main(String[] args) {
int[] xy = new int[2];
System.out.println(gcdExtended(42, 2017, xy));
System.out.println(modInverse(42, 2017));
System.out.println(modInverse(3, 7));
System.out.println(Arrays.toString(xy));
long ans = 0;
for (int i : primesUpTo(2000000))
ans += i;
System.out.println(ans);
}
}