Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Project Euler yet again
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Panphobia




PostPosted: 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
Sponsor
sponsor
Insectoid




PostPosted: 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




PostPosted: 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.
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
Panphobia




PostPosted: Tue Jan 01, 2013 11:03 pm   Post subject: RE:Project Euler yet again

Ohhh yeaa sorry about that, it is kind of a short program, doesn't require it, but it isn't good programming practice I know.
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 4 Posts ]
Jump to:   


Style:  
Search: