- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproblem002.cpp
More file actions
Latest commit
27 lines (22 loc) · 552 Bytes
/
Copy pathproblem002.cpp
File metadata and controls
27 lines (22 loc) · 552 Bytes
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
#include<cmath>
#include<iostream>
constdouble phi = (1 + sqrt(5)) / 2;
intfibIndex(int n)
{
returnlog(n * sqrt(5) + 0.5) / log(phi);
}
intfib(int n)
{
// Accurate only up to the 70th term due to the precision of double.
return (pow(phi, n) - pow(-phi, -n)) / sqrt(5) + 0.5;
}
intsumOfEvenFib(int upper)
{
// I.e., the sum of every 3rd Fibonacci term up to the given limit.
return (fib(3 * (fibIndex(upper) / 3) + 2) - 1) / 2;
}
intmain()
{
std::cout << "Answer: " << sumOfEvenFib(4000000) << '\n';
return0;
}