As an independent study assignment, I registered for this website called Project
ID: 440305 • Letter: A
Question
As an independent study assignment, I registered for this website called ProjectEuler. I am trying the second problem and I am kind of confused. Here is the website: http://projecteuler.net/problem=2 and here is my code: public class Fibonacci { public static void main(String [] args){ int a=1; int b=2; int c= a+ b; int d=1; int f=1; int g=1; int[] c1=new int[1000]; while (true){ c1[1]=1; c1[2]=2; c1[3]=c; c1[4]=c1[3]+c1[2]; c1[4+d]=c1[4+d-1]+ c1[4+d-2]; while (c1[4+d]> 2){ f= c1[4+d]; g= c1[4+d-1]; System.out.println(f); d=d+1; c1[4+d]=f+g; } } } } The first thing I tried to do was list out all the numbers in the Fibonacci sequence. But my question is, how does the JVM know what to put in the variable g because the variable f holds the "next" number and g holds the "previous" number and the previous number isn't stored anywhere. But somehow, once you compile this, it actually does give you the sequence of numbers. Can you please help me with this?Explanation / Answer
This is the second Project Euler Challenge. Well I can tell you that the answer is indeed 4613732. You are asked to find the SUM of all the even valued numbers in the Fibonacci sequence below 4 million. Try my solution. This also works int total = 0; int max = 4000000; int next = 1; int last = 1; while (nextRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.