-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path002.cs
More file actions
67 lines (56 loc) · 1.53 KB
/
Copy path002.cs
File metadata and controls
67 lines (56 loc) · 1.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
// Problem 2
//
// 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.
namespace _002
{
class Program
{
static double[] lookup = new double[101];
static void Main(string[] args)
{
for (int i = 0; i < lookup.Length; i++ )
{
lookup[i] = 0;
}
int x = 0;
double sum = 0;
bool continuea = true;
while (continuea)
{
double nextFib = fib(x);
if (nextFib > 4000000)
continuea = false;
if (nextFib % 2 == 0)
sum += nextFib;
x++;
}
Console.WriteLine(sum);
}
static double fib(int x)
{
if (x == 0)
return 0;
else if (x == 1)
return 1;
else
{
return calculateFib(x - 1) + calculateFib(x - 2);
}
}
static double calculateFib(int x)
{
if (lookup[x] == 0)
lookup[x] = fib(x);
return lookup[x];
}
}
}