Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Overflow and time-efficient in a for-loop?

  1. #1
    Junior Member
    Join Date
    Jun 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Overflow and time-efficient in a for-loop?

    Project Euler, problem 2: Determine the sum of the even numbers in the Fibonacci sequence up to 4 000 000. First I tried to
    use a recursive algorithm for the sequence but I realized I dont have all the time in the world so I now use an iterative. It is still
    extremely slow. Can I improve my code?

     
     
     
    public class Euler2Correct {
     
        public static int Fibonacci(int j){ 
     
            /**
             * Metod for returnerning number [I]j[/I] in the sequence.
             * 
             */
     
            if(j<=1){
                return 1;
            }
            else if(j==2){
                return 2;
            }
     
            int tmp;
            int a=2;
            int b=1;
     
            for(int k=3; k<=j; k++){
     
                tmp=a+b;
                b=a;
                a=tmp;
     
            }
     
            return a;
     
        }
     
        public static void main(String[]args){
     
           int s=0;
     
            for(int i=2; i<4000000; i=i+3){ //Every three number is even
     
                s = s + Fibonacci(i);
     
            }
            System.out.println(s);
     
        }  
    }


  2. #2
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    849
    Thanks
    0
    Thanked 158 Times in 141 Posts

    Default Re: Overflow and time-efficient in a for-loop?

    You could read up on dynamic programming if you want to get it much faster, but I am not going to explain it in this post, it would be a little bit too much.

  3. #3
    Junior Member
    Join Date
    Jun 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Overflow and time-efficient in a for-loop?

    Quote Originally Posted by Cornix View Post
    You could read up on dynamic programming if you want to get it much faster, but I am not going to explain it in this post, it would be a little bit too much.
    I was more concerned about obvious flaws and time-consuming operations(like the recursive way, although simpler). I went through the Wiki page and it looks interesting but I doubt that it requires that kind of knowledge. Just something easy and neat.

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,355
    Thanks
    182
    Thanked 833 Times in 776 Posts
    Blog Entries
    5

    Default Re: Overflow and time-efficient in a for-loop?

    Think about your algorithm or - even better - write down the calculations. Or just let the program do it for you by adding some printlns. Here's a result of doing just that - where each line represents the calculations done per call of the fibonacci method:

    2 + 1,3 + 2,5 + 3
    2 + 1,3 + 2,5 + 3,8 + 5,13 + 8,21 + 13
    2 + 1,3 + 2,5 + 3,8 + 5,13 + 8,21 + 13,34 + 21,55 + 34,89 + 55
    2 + 1,3 + 2,5 + 3,8 + 5,13 + 8,21 + 13,34 + 21,55 + 34,89 + 55,144 + 89,233 + 144,377 + 233
    2 + 1,3 + 2,5 + 3,8 + 5,13 + 8,21 + 13,34 + 21,55 + 34,89 + 55,144 + 89,233 + 144,377 + 233,610 + 377,987 + 610,1597 + 987

    See any calculations being repeated again and again?

  5. #5
    Junior Member
    Join Date
    Jun 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Overflow and time-efficient in a for-loop?

    Yes, this was a good observation. So I better keep the running total in the Fibonacci method, and use the previous results to prevent doing all that extra work. I'll modify it.

Similar Threads

  1. Making a Graphics Drawing Loop More Efficient
    By Gravity Games in forum Loops & Control Statements
    Replies: 0
    Last Post: July 5th, 2013, 04:40 PM
  2. Ways to make this loop more efficient?
    By java-noobette in forum What's Wrong With My Code?
    Replies: 4
    Last Post: April 8th, 2013, 01:20 PM
  3. While loop only runs one time?
    By Purple01 in forum Java Theory & Questions
    Replies: 2
    Last Post: November 9th, 2012, 09:36 AM
  4. For loop, the first command in the loop does not get executed the 2nd time..
    By lina_inverse in forum Loops & Control Statements
    Replies: 1
    Last Post: October 16th, 2012, 10:00 PM
  5. Loop only runs one time?
    By Purple01 in forum Loops & Control Statements
    Replies: 6
    Last Post: September 14th, 2012, 07:57 AM