Posted: Tue Jan 01, 2013 6:06 pm Post subject: Project Euler yet again
I have been doing project euler a lot, it is addicting, I am stuck on making this question solution efficient http://projecteuler.net/problem=148 , I keep getting the java heap space exception or whatever, here is my code
code:
package projecteuler148;
import java.util.*;
import java.math.*;
public class ProjectEuler148 {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
BigInteger lol;
int tot = 0;
ArrayList<BigInteger>fact=new ArrayList<BigInteger>();
fact.add(BigInteger.ONE);
out:
for (int i = 0; i < Math.pow(10, 9); ++i) {
for (int x = 0; x < i + 1; ++x) {
fact.add(BigInteger.valueOf(i+1).multiply(fact.get(i)));
lol = fact.get(i).divide(fact.get(i - x).multiply(fact.get(x)));
if (lol.equals(BigInteger.ZERO)) {
continue out;
}
}
tot++;
}
System.out.println(tot);
}
}
I do not know the better solution to this, is there a more efficient way to do this than what I am doing atm?
Sponsor Sponsor
Insectoid
Posted: Tue Jan 01, 2013 6:24 pm Post subject: RE:Project Euler yet again
This is a problem better solved with math than code. They're asking to check the first one billion rows specifically so your solution will not work. I suggest you study Pascal's triangle for a bit to discover any shortcuts you could take.
Tony
Posted: Tue Jan 01, 2013 8:47 pm Post subject: Re: Project Euler yet again
Panphobia @ Tue Jan 01, 2013 6:06 pm wrote:
BigInteger lol;
This probably doesn't matter to you, but to anyone else reading your code this naming convention works very poorly.