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 6126225
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T16:20:44+00:00 2026-05-23T16:20:44+00:00

I am trying to solve Project Euler problem 2 in Java: public class Euler2

  • 0

I am trying to solve Project Euler problem 2 in Java:

public class Euler2 {

public static long GenerateFibonacci(int term) {

    long sum = 0;
    long fib = 0;
    long f1 = 0;
    long f2 = 1;
    if (term <=1) return term;
    for (int i = 1; i <= term; i++) {
        fib = f1 + f2;
        f1 = f2;
        f2 = fib;
        if(fib %2 ==0)
            sum += fib;         
    }
    return sum;

}

    /**
     * @param args
     */
    public static void main(String[] args) {

        int n = 100;
        long result = GenerateFibonacci(n);
        System.out.println("The sum of the even Fibonacci numbers is: "+result);
    }
}

When n is small I get the right answer but for bigger values I get the wrong result. What’s the problem here?

  • 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-05-23T16:20:45+00:00Added an answer on May 23, 2026 at 4:20 pm

    int is limited to 32-bit accuracy, long to 64-bit.

    When you exceed the limit by adding numbers whose result is larger then the bit limit, they “roll over” and you lose the most significant bits from the result of the addition – essentially, they are “rounded” to 32/64 bits.

    Here’s an example of rolling over:

    int i = Integer.MAX_VALUE; // 2147483647
    i++; // -2147483648
    

    Roughly speaking, each fibonnacci number is double the previous one, so roughly speaking you can only handle in the order of 64 iterations using a long as the total.

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

Sidebar

Related Questions

I'm trying to solve Problem 41 of project Euler in Java, by counting the
I'm trying to solve Project Euler Problem 14 in a lazy way. Unfortunately, I
I have been trying to solve Project Euler's problem #59 for a while, and
I'm trying to solve problem 50 on Project Euler . Don't give me the
I'm trying to solve Problem #5 in Project Euler. The code works for the
I am trying to solve Project Euler problem #12: The sequence of triangle numbers
I'm trying to solve the 8th problem of the project Euler and I'm stuck
For a little context, i'm trying to solve Project Euler problem 31 using excellent
I'm currently trying to solve problem 18 of project Euler . The goal is:
I've been trying to solve Problem 20 in Project Euler: n! means n (n

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.