-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPEProb500
115 lines (103 loc) · 3.64 KB
/
PEProb500
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
package com.signature;
public class Main {
public static void main(String[] args) {
long[] factors = new long[1000000]; //array of prime factors
long[] factorsPower = new long[1000000]; //array of prime factors raised to current power
long[] powers = new long[1000000]; //array of exponents for corresponding prime factor
long marker = 0; //marker to see largest prime factor raised
long[] answer = new long[2];
initializeFactors(factorsPower, factors); //initialize prime factors array
initializePowers(powers); //initialize powers array with all 1s instead of 0s
System.out.println("Arrays Initialized to " + factors[powers.length - 1]);
while (calcNumFactors(powers) < 500500) {
marker = findMinFactor(factorsPower, powers, marker);
}
answer = findProduct(factors, powers, marker);
System.out.println("Product: " + answer[0] % 500500507);
}
public static long[] initializeFactors(long[] factors, long[] factorsPower) {
int i = 0;
long x = 2;
while (i < factors.length) {
if (isPrime(x)) {
factors[i] = x;
factorsPower[i] = x;
i++;
x++;
} else {
x++;
}
}
return factors;
}
public static Boolean isPrime(long a) {
for (int i = 2; i <= Math.sqrt(a); i++) {
if (a % i == 0) {
return false;
}
}
return true;
}
public static long[] initializePowers(long[] powers) {
for (int i = 0; i < powers.length; i++) {
powers[i] = 1;
}
return powers;
}
public static long calcNumFactors(long[] powers) {
long x = 1;
long j = 0;
for (int i = 0; i < powers.length; i++) {
x = x * powers[i];
while (x > 1) {
x /= 2;
j = j + 1L;
}
}
return j;
}
public static long findMinFactor(long[] numPower, long[] power, long marker) {
long minFactor = Long.MAX_VALUE;
int minPlace = 0;
for (int i = 0; i <= marker + 1; i++) {
if (numPower[i] < minFactor) {
minFactor = numPower[i];
minPlace = i;
if (i > marker) {
marker = i;
}
}
}
numPower[minPlace] *= numPower[minPlace]; //update numPower
power[minPlace] = power[minPlace] * 2; //update powers array
return marker;
}
public static long[] power(long x, long y, long overflow) {
long[] retArray = new long[]{0, 0};
long j = 1;
for (int i = 0; i < y; i++) {
j = j * x;
overflow += j / 500500507;
j %= 500500507;
}
retArray[0] = overflow;
retArray[1] = j;
return retArray;
}
public static long[] findProduct(long[] factors, long[] powers, long marker) {
long p = 1L;
long overflow = 0;
long[] handleArray = new long[]{0, 0};
long[] retArray = new long[]{0, 1};
for (int i = 0; i < marker + 1; i++) {
overflow += p / 500500507;
p %= 500500507;
handleArray = power(factors[i], powers[i] - 1, overflow);
p *= handleArray[1];
overflow = handleArray[0];
}
retArray[0] = p;
retArray[1] = overflow;
return retArray;
}
}