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

  • Home
  • SEARCH
  • 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 8120005
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T04:59:06+00:00 2026-06-06T04:59:06+00:00

This is from project Euler, problem 2. I wrote the following seemingly innocent code:

  • 0

This is from project Euler, problem 2. I wrote the following seemingly innocent code:

public class FibonacciEven {
    public static void main(String[] stuff) {
        long sum = 0;
        int i = 0;
        while(fib(i) <= 40) {
            boolean even = fib(i) % 2 == 0;
            if(even) {
                sum += fib(i);
            }
            else {
                continue;
            }
            i++;
        }
        System.out.println(sum);
    }
    public static long fib(int n) {
        long prev1 = 0;
        long prev2 = 1;
        for(int i = 0; i < n; i++) {
            long savePrev1 = prev1;
            prev1 = prev2;
            prev2 = savePrev1 + prev2;
        }
        return prev1;
    }
}

I did read about how the Java method for working out Fibonacci numbers is quite memory hungry, but, as you can see, I scaled my limit down to 40, and it still doesn’t get to the end, so I assume that I have gotten some syntax horribly wrong. What bit of the code is making it run forever? And if all of this really is due to the fact that the method takes so much time to run, can anyone suggest a better way?

EDIT: ok, now my code looks like this:

public class FibonacciEven {
    public static void main(String[] stuff) {
        long sum = 0;
        int i = 0;
        while(fib(i) <= 40) {
            boolean even = fib(i) % 2 == 0;
            if(even) {
                sum += fib(i);
            }
            i++;
        }
        System.out.println(sum);
    }
    public static long fib(int n) {
        long prev1 = 0;
        long prev2 = 1;
        for(int i = 0; i < n; i++) {
            long savePrev1 = prev1;
            prev1 = prev2;
            prev2 = savePrev1 + prev2;
        }
        return prev1;
    }
}

This time it ignores the 2 (index 3) in the Fibonacci sequence.

  • 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-06T04:59:09+00:00Added an answer on June 6, 2026 at 4:59 am

    If even is false, you end up continuin without updating i – so it’ll loop round again and do exactly the same work again, so even will be false again, etc…

    I suspect you just want to take out the else block.

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

Sidebar

Related Questions

Spoiler alert: this is related to Problem 14 from Project Euler. The following code
This is Problem 3 from Project Euler site I'm not out after the solution,
As some of you may notice this question is problem 16 from Project Euler
This is a problem from euler-project. No.13 import math #no.13 sum = [] number
Imagine this as the code from build.xml: <project name=test project> <target name=first> <echo>first</echo> </target>
So on Project Euler the Problem 4 states the following: A palindromic number reads
I am learning python through the Project Euler problems. For problem 40 I wrote
Possible Duplicate: Need help solving Project Euler problem 200 Similar to this question Project
I have the following (correct) solution to Project Euler problem 24. I'm relatively new
I did this problem [ Project Euler problem 5 ], but very bad manner

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.