|
|
|
@ -3,6 +3,7 @@ import java.math.BigInteger;
@@ -3,6 +3,7 @@ import java.math.BigInteger;
|
|
|
|
|
import java.util.List; |
|
|
|
|
import java.util.Map; |
|
|
|
|
import java.util.TreeMap; |
|
|
|
|
import java.util.HashMap; |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Find the product of the first 500500 primes. |
|
|
|
@ -15,7 +16,7 @@ public class BigMultiply {
@@ -15,7 +16,7 @@ public class BigMultiply {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public static void solve() { |
|
|
|
|
int N1 = 500500; |
|
|
|
|
int N1 = 100500; |
|
|
|
|
int N2 = 500500507; |
|
|
|
|
//int N2fac1 = 13;
|
|
|
|
|
//int N2fac2 = 38500039;
|
|
|
|
@ -29,6 +30,11 @@ public class BigMultiply {
@@ -29,6 +30,11 @@ public class BigMultiply {
|
|
|
|
|
// This will store the number of times we have used each prime
|
|
|
|
|
TreeMap<Long,Integer> primeUsage = new TreeMap<Long,Integer>(); |
|
|
|
|
|
|
|
|
|
// This will memoize power of prime:
|
|
|
|
|
// each key is the prime,
|
|
|
|
|
// each value is the prime to its power of occurrence
|
|
|
|
|
HashMap<Long,Long> primePower = new HashMap<Long,Long>(); |
|
|
|
|
|
|
|
|
|
BigInteger b = BigInteger.ONE; |
|
|
|
|
BigInteger prime = BigInteger.ONE; |
|
|
|
|
|
|
|
|
@ -42,6 +48,9 @@ public class BigMultiply {
@@ -42,6 +48,9 @@ public class BigMultiply {
|
|
|
|
|
// - Multiply by a prime we have not used before
|
|
|
|
|
// - Multiply by a prime that we have used k times before, k+1 times
|
|
|
|
|
// Both of these will double the number of divisors.
|
|
|
|
|
if(i%1000==0) { |
|
|
|
|
System.out.println("On divisor 2^"+i); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Get next prime
|
|
|
|
|
nextPrime = primesList.get(primeIndex); |
|
|
|
@ -51,7 +60,7 @@ public class BigMultiply {
@@ -51,7 +60,7 @@ public class BigMultiply {
|
|
|
|
|
boolean useOptionA = true; |
|
|
|
|
|
|
|
|
|
for(Long primeSoFar : primeUsage.keySet()) { |
|
|
|
|
optionB = longpow( primeSoFar, primeUsage.get(primeSoFar)+1 ); |
|
|
|
|
optionB = primePower.get(primeSoFar); |
|
|
|
|
//System.out.println("Checking option B: primeSoFar = "+primeSoFar+" current count = "+ (primeUsage.get(primeSoFar)) +" option B = "+optionB);
|
|
|
|
|
if(optionB<optionA) { |
|
|
|
|
// Use option B
|
|
|
|
@ -61,6 +70,9 @@ public class BigMultiply {
@@ -61,6 +70,9 @@ public class BigMultiply {
|
|
|
|
|
//System.out.println("Incrementing value of prime "+primeSoFar+" by "+ (value+1) +" to "+ ((value+1)+value) );
|
|
|
|
|
value = value + (value+1); |
|
|
|
|
primeUsage.put(primeSoFar,value); |
|
|
|
|
|
|
|
|
|
// memoize the power we will see next time
|
|
|
|
|
primePower.put(primeSoFar, longpow(primeSoFar, value + (value+1) ) ); |
|
|
|
|
|
|
|
|
|
// Multiply
|
|
|
|
|
b = b.multiply( BigInteger.valueOf(optionB) ).mod(base); |
|
|
|
@ -74,6 +86,10 @@ public class BigMultiply {
@@ -74,6 +86,10 @@ public class BigMultiply {
|
|
|
|
|
primeUsage.put(optionA, new Integer(1)); |
|
|
|
|
//System.out.println("Initializing value of prime "+optionA+" to 1.");
|
|
|
|
|
|
|
|
|
|
// add a memoized version of what the power
|
|
|
|
|
// will be next time (2)
|
|
|
|
|
primePower.put(optionA, optionA*optionA); |
|
|
|
|
|
|
|
|
|
// Multiply
|
|
|
|
|
b = b.multiply( BigInteger.valueOf(optionA) ).mod(base); |
|
|
|
|
//System.out.println("A: Next factor is "+optionA);
|
|
|
|
|