class Program
{
static void Main(string[] args)
{
Console.WriteLine("Fib 1: ");
Console.ReadLine();
}
long fibonacci1()
{
long a = 1, b = 2, c, answer = 0;
for (int i = 0; answer < 4000000; i++)
{
c = a + b;
if (c % 2 == 0)
{
answer += c;
}
a = b + c;
if (a % 2 == 0)
{
answer += a;
}
b = a + c;
if (b % 2 == 0)
{
answer += b;
}
}
return answer;
}
void fibonacci2()
{
long[] y = new long [1000000];
long x = 2;
long a = y[x - 2] = 1;
long b = y[x - 1] = 2;
long n = y[x];
long answer = 0;
for (x=2; answer < 4000000; x++)
{
n = a + b;
if(n % 2 == 0)
{
answer += n;
}
}
Console.WriteLine("Fib 2: " + answer);
}
}
This is what I came up with so far. I am trying to come up with 2 ways to come up with the answer.
1) How do you call the two methods?
2) What you guys think about the two ways? I couldn’t test it, but any advice or hints(if I am wrong) Don’t give me the answer:)
Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be:
1, 2, 3, 5, 8, 13, 21, 34, 55, 89, …
By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.
Make them both
staticfunctions like this:and call them from
Main()like this: