Skip to content

Commit 6df59cd

Browse files
Abhishek JainAbhishek Jain
authored andcommitted
Added O(1) Sum of Even Terms of Fibonacci Series
1 parent 0aba6d4 commit 6df59cd

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

Project-Euler/Problem2.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
const SQ5 = 5 ** .5; // Square root of 5
2+
const PHI = (1 + SQ5) / 2; // definition of PHI
3+
4+
//theoretically it should take O(1) constant amount of time as long
5+
// arithmetic calculations are considered to be in constant amount of time.
6+
7+
function EvenFibonacci(limit) {
8+
const highestIndex = Math.floor(Math.log(limit * SQ5) / Math.log(PHI));
9+
const n = Math.floor(highestIndex / 3);
10+
return ((PHI ** (3 * n + 3) - 1) / (PHI ** 3 - 1)
11+
- ((1 - PHI) ** (3 * n + 3) - 1) / ((1 - PHI) ** 3 - 1)) / SQ5;
12+
}
13+
14+
console.log(EvenFibonacci(4e6));
15+
// Sum of Even Fibonnaci upto 4 Million

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy