Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8924489
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T07:25:53+00:00 2026-06-15T07:25:53+00:00

I have two solutions for Project Euler question 2, namely find the sum of

  • 0

I have two solutions for Project Euler question 2, namely find the sum of all even Fibonacci numbers less than 4 million.

Solution one (which takes an average of 11,000 nanoSeconds):

public class Solution {

static long startTime = System.nanoTime();
static final double UPPER_BOUND = 40e5;
static int sum = 2;

public static int generateFibNumber(int number1, int number2){
    int fibNum = number1+ number2;
    return fibNum;
}

public static void main( String args[] ) {
    int i = 2;
    int prevNum = 1;
    while(i <= UPPER_BOUND) {
        int fibNum = generateFibNumber(prevNum,i);
        prevNum = i;
        i = fibNum;
        if (fibNum%2 == 0){
            sum += fibNum;
        }
    }
    long stopTime = System.nanoTime();
    long time = stopTime - startTime;
    System.out.println("Sum: " + sum);
    System.out.println("Time: "+ time);
}

and solution two(which takes an average of 14,000 nanoseconds):

public class Solution2 {
static long startTime = System.nanoTime();  
final static int UPPER_BOUND = 4_000_000;
static int penultimateTerm = 2;                                         
static int prevTerm = 8;                                                
static int currentTerm = 34;                                             
static int sum = penultimateTerm+ prevTerm;                 

public static void main( String args[]) {
    while (currentTerm <= UPPER_BOUND) {
        sum+= currentTerm;
        penultimateTerm = prevTerm;
        prevTerm = currentTerm;
        currentTerm = (4*prevTerm) + penultimateTerm;
    }

    long stopTime = System.nanoTime();
    long time = stopTime - startTime;
    System.out.println("Sum: " + sum);
    System.out.println("Time: " + time);

}

Why is solution two taking longer when I am performing way fewer iterations inside the while loop and and also do not have an if statement?
Can this be done more efficiently?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T07:25:54+00:00Added an answer on June 15, 2026 at 7:25 am

    Running your algorithm only once is a highly unreliable way of evaluating its performance, particularly when the times are on the order of 10ns. Your second method is, indeed, faster. I rewrote your code to iterate each algorithm 100 times and got quite different results from you.

    Code:

    public class Fib {
        private static int UPPER_BOUND = 4000000;
        private static int ITERS = 100;
        public static void main(String[] args) {
            long time1, time2;
            int sum1 = 0, sum2 = 0;
            long startTime = System.nanoTime();
            for (int iter = 0; iter < ITERS; ++iter) {
                sum1 = sol1();
            }
            time1 = System.nanoTime() - startTime;
    
            startTime = System.nanoTime();
            for (int iter = 0; iter < ITERS; ++iter) {
                sum2 = sol2();
            }
            time2 = System.nanoTime() - startTime;
            System.out.println("Time1 = " + time1 + "; sum1 = " + sum1);
            System.out.println("Time2 = " + time2 + "; sum2 = " + sum2);
        }
    
        private static int sol1() {
            int sum = 2;
            int i = 2;
            int prevNum = 1;
            while(i <= UPPER_BOUND) {
                int fibNum = generateFibNumber(prevNum,i);
                prevNum = i;
                i = fibNum;
                if (fibNum%2 == 0){
                    sum += fibNum;
                }
            }
            return sum;
        }
    
        private static int sol2() {
            int penultimateTerm = 2;
            int prevTerm = 8;
            int currentTerm = 34;
            int sum = penultimateTerm + prevTerm;
            while (currentTerm <= UPPER_BOUND) {
                sum += currentTerm;
                penultimateTerm = prevTerm;
                prevTerm = currentTerm;
                currentTerm = (prevTerm << 2) + penultimateTerm;
            }
            return sum;
        }
    
        private static int generateFibNumber(int number1, int number2) {
            return number1+ number2;
        }
    }
    

    Results (typical):

    Time1 = 189910; sum1 = 4613732
    Time2 = 35501; sum2 = 4613732

    Note that in the second algorithm, I changed (4*prevTerm) with (prevTerm << 2), which is slightly faster. This improved the time by about 5%. There’s still a lot of overhead in each test: a function call and assigning the result to a local variable. However, by iterating you aren’t down in the noise in your calls to System.nanoTime().

    Note that your first code was also using a double for UPPER_BOUND, which slows it down a bit. My code tried to make the tests as parallel as possible.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose my solution have two project: myApp: silverlight application project: the default application App
Need to add two same name .csproj class libraries in my solution.Have two project
I have two projects in my solution   1- asp.net web project.   2-
I have two projects in my solution... a domain project and MVC3 web project
I have a solution with two projects. One for project for the production code
I have a few winform projects in different solutions. If I have two winform
I have two solutions in my workspace, say A and B. Solution A is
I have two Visual Studio solutions, one for WebApi and one for WP7 development,
I have two C++/CLI projects A and B in separate solutions. I use A
I have two scenarios: There is a Framework project for the company, and for

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.