So I've been doing Project Euler, and I found two ways to solve Problem 57. However, there seems to be a huge difference in performance between using a 32bit JDK and a 64bit JDK.
public static int fractionSolution (int l){
int count = 0;
for (int i = 1; i < l; i++) {
BigFraction f = iterateFraction(i).subtract(new BigFraction(1,1));
if(f.getDenominator().toString().length() < f.getNumerator().toString().length()){
count++;
}
}
return count;
}
public static BigFraction iterateFraction(int n){
if(n==1){
return new BigFraction(2.5);
}
else{
BigFraction base = new BigFraction(1,1);
BigFraction two = new BigFraction(2,1);
return two.add(base.divide(iterateFraction(n-1)));
}
}
public static int patternSolution (int l){
BigInteger n = new BigInteger("3");
BigInteger d = new BigInteger("2");
int count = 0;
for (int i = 1; i < l; i++) {
n = n.add(d.multiply(new BigInteger("2")));
d = n.subtract(d);
if(n.toString().length() > d.toString().length()){
count++;
}
}
return count;
}
If I run the fractionSolution on 64 bit JDK, it will take 30 seconds, but on 32bit, it will take 90 seconds.
If I run patternSolution on 64 bit JDK, it will take about 80 ms, but on 32 bit, it will take about 40 ms.
Why is there such a huge difference between the JDKs? Which one should I use?
I'm using Java SE 7, JDK 1.7
Here are screen caps of my program so you don't have to run it. https://i.stack.imgur.com/BeS8F.png
You can tell which JDK it is from the file paths.