-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2_even_fib.cpp
67 lines (61 loc) · 1016 Bytes
/
2_even_fib.cpp
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
#include <iostream>
#include <cmath>
#include <iomanip>
const int width = 170;
const int numw = 10;
const int limit = 4000000;
using namespace std;
long long naive();
long long better(); //Every third fibonacci number is even
long long even_better(); //Feven(N) = 4Feven(n-1)+Feven(n-2)
int main()
{
cout<<setw(5)<<naive()<<endl;
cout<<setw(5)<<better()<<endl;
cout<<setw(5)<<even_better()<<endl;
}
long long naive()
{
long long a,b,c,sum,i;
a = b = 1;
i = sum = 0;
c = 2;
while(c<limit)
{
i++;
sum += (c%2==0)?(c):(0);
a = b;
b = c;
c = b+a;
}
return sum;
}
long long better() //Every third fibonacci number is even
{
long long a,b,c,sum=0;
a = b = 1;
c = 2;
while(c<limit)
{
sum+=c;
a = b+c;
b = a+c;
c = a+b;
}
return sum;
}
long long even_better() //Feven(N) = 4Feven(n-1)+Feven(n-2)
{
long long a,b,c,sum=0;
a=0;
b=2;
c=4*b+a;
while(c<limit)
{
sum+=c;
a=b;
b=c;
c=4*b+a;
}
return sum;
}