From 6df59cde45dc2e288c06445bef57847f719c6e02 Mon Sep 17 00:00:00 2001 From: Abhishek Jain Date: Sat, 10 Oct 2020 18:06:37 +0530 Subject: [PATCH 1/3] Added O(1) Sum of Even Terms of Fibonacci Series --- Project-Euler/Problem2.js | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 Project-Euler/Problem2.js diff --git a/Project-Euler/Problem2.js b/Project-Euler/Problem2.js new file mode 100644 index 000000000..ed3c30c7c --- /dev/null +++ b/Project-Euler/Problem2.js @@ -0,0 +1,15 @@ +const SQ5 = 5 ** .5; // Square root of 5 +const PHI = (1 + SQ5) / 2; // definition of PHI + +//theoretically it should take O(1) constant amount of time as long +// arithmetic calculations are considered to be in constant amount of time. + +function EvenFibonacci(limit) { + const highestIndex = Math.floor(Math.log(limit * SQ5) / Math.log(PHI)); + const n = Math.floor(highestIndex / 3); + return ((PHI ** (3 * n + 3) - 1) / (PHI ** 3 - 1) + - ((1 - PHI) ** (3 * n + 3) - 1) / ((1 - PHI) ** 3 - 1)) / SQ5; +} + +console.log(EvenFibonacci(4e6)); +// Sum of Even Fibonnaci upto 4 Million \ No newline at end of file From 10f664d63f9e975f33b873ea2f62851c29a14417 Mon Sep 17 00:00:00 2001 From: Abhishek Jain Date: Sat, 10 Oct 2020 18:14:29 +0530 Subject: [PATCH 2/3] ESLINT fixes --- Project-Euler/Problem2.js | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/Project-Euler/Problem2.js b/Project-Euler/Problem2.js index ed3c30c7c..275686d87 100644 --- a/Project-Euler/Problem2.js +++ b/Project-Euler/Problem2.js @@ -1,15 +1,17 @@ -const SQ5 = 5 ** .5; // Square root of 5 -const PHI = (1 + SQ5) / 2; // definition of PHI +const SQ5 = 5 ** 0.5 +//Square root of 5 + +const PHI = (1 + SQ5) / 2 +// definition of PHI //theoretically it should take O(1) constant amount of time as long -// arithmetic calculations are considered to be in constant amount of time. - +// arithmetic calculations are considered to be in constant amount of time function EvenFibonacci(limit) { - const highestIndex = Math.floor(Math.log(limit * SQ5) / Math.log(PHI)); - const n = Math.floor(highestIndex / 3); - return ((PHI ** (3 * n + 3) - 1) / (PHI ** 3 - 1) - - ((1 - PHI) ** (3 * n + 3) - 1) / ((1 - PHI) ** 3 - 1)) / SQ5; + const highestIndex = Math.floor(Math.log(limit * SQ5) / Math.log(PHI)) + const n = Math.floor(highestIndex / 3) + return ((PHI ** (3 * n + 3) - 1) / (PHI ** 3 - 1) - + ((1 - PHI) ** (3 * n + 3) - 1) / ((1 - PHI) ** 3 - 1)) / SQ5 } console.log(EvenFibonacci(4e6)); -// Sum of Even Fibonnaci upto 4 Million \ No newline at end of file +// Sum of Even Fibonnaci upto 4 Million From 9e6fe4bdaff04d7abf594e3125d5d45abba2d23e Mon Sep 17 00:00:00 2001 From: Abhishek Jain Date: Sat, 10 Oct 2020 18:18:25 +0530 Subject: [PATCH 3/3] Fixed Indendation Issues --- Project-Euler/Problem2.js | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/Project-Euler/Problem2.js b/Project-Euler/Problem2.js index 275686d87..cc6f2cea0 100644 --- a/Project-Euler/Problem2.js +++ b/Project-Euler/Problem2.js @@ -1,17 +1,16 @@ const SQ5 = 5 ** 0.5 -//Square root of 5 +// Square root of 5 const PHI = (1 + SQ5) / 2 // definition of PHI -//theoretically it should take O(1) constant amount of time as long +// theoretically it should take O(1) constant amount of time as long // arithmetic calculations are considered to be in constant amount of time -function EvenFibonacci(limit) { +function EvenFibonacci (limit) { const highestIndex = Math.floor(Math.log(limit * SQ5) / Math.log(PHI)) const n = Math.floor(highestIndex / 3) return ((PHI ** (3 * n + 3) - 1) / (PHI ** 3 - 1) - ((1 - PHI) ** (3 * n + 3) - 1) / ((1 - PHI) ** 3 - 1)) / SQ5 } - -console.log(EvenFibonacci(4e6)); +console.log(EvenFibonacci(4e6)) // Sum of Even Fibonnaci upto 4 Million