From f97bdc3414b43ece30f45e3365acff2dd96612e6 Mon Sep 17 00:00:00 2001 From: Adhiraj Date: 2021年10月16日 14:38:02 +0530 Subject: [PATCH 1/9] added Problem 25 --- Project-Euler/Problem025.js | 59 +++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 Project-Euler/Problem025.js diff --git a/Project-Euler/Problem025.js b/Project-Euler/Problem025.js new file mode 100644 index 0000000000..7daaf9e91b --- /dev/null +++ b/Project-Euler/Problem025.js @@ -0,0 +1,59 @@ +/** +* Problem 20 - 1000-digit Fibonacci number +* +* @see {@link https://projecteuler.net/problem=25} +* +* The Fibonacci sequence is defined by the recurrence relation: +* +* Fn = Fn−1 + Fn−2, where F1 = 1 and F2 = 1. +* +* Hence the first 12 terms will be: +* +* F1 = 1 +* F2 = 1 +* F3 = 2 +* F4 = 3 +* F5 = 5 +* F6 = 8 +* F7 = 13 +* F8 = 21 +* F9 = 34 +* F10 = 55 +* F11 = 89 +* F12 = 144 +* The 12th term, F12, is the first term to contain three digits. + +* What is the index of the first term in the Fibonacci sequence to contain 1000 digits? +*/ + +// brute force method + +function fibonacci(n) { + // Creates an array of Fibonacci numbers using the Fibonacci formula. Returns the nth element of the array. + if (n === 1) { + return 0 + } + else if (n === 2) { + return 1 + } + else { + series = [0,1] + for (let i = 2; i <= n; i++) { + series.push(sequence[i-1]+sequence[i-2]) + } + return sequence[n] + } +} + +function fibonacciIndex(n = 1000) { + // Computes incrementing Fibonacci numbers starting from 3 and checks if its length is equal to n. Returns the term of the sequence in which it happens first. + let digits = 0 + let index = 2 + while (digits < n) { + index += 1 + digits = fibonacci(index).toString().length + } + return index +} + +export { fibonacciIndex } \ No newline at end of file From 25ea19b82a7b3e1ffc3b561dbd517c70aa05e732 Mon Sep 17 00:00:00 2001 From: Adhiraj Date: 2021年10月16日 14:48:12 +0530 Subject: [PATCH 2/9] fixed alert --- Project-Euler/Problem025.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Project-Euler/Problem025.js b/Project-Euler/Problem025.js index 7daaf9e91b..4300bf99cf 100644 --- a/Project-Euler/Problem025.js +++ b/Project-Euler/Problem025.js @@ -37,7 +37,7 @@ function fibonacci(n) { return 1 } else { - series = [0,1] + let series = [0,1] for (let i = 2; i <= n; i++) { series.push(sequence[i-1]+sequence[i-2]) } @@ -56,4 +56,4 @@ function fibonacciIndex(n = 1000) { return index } -export { fibonacciIndex } \ No newline at end of file +export { fibonacciIndex } From 44625e4987f6724bb9785514595490c4aae079fb Mon Sep 17 00:00:00 2001 From: Adhiraj Date: 2021年10月16日 15:06:04 +0530 Subject: [PATCH 3/9] fixed alert --- Project-Euler/Problem025.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Project-Euler/Problem025.js b/Project-Euler/Problem025.js index 4300bf99cf..048d43ddd4 100644 --- a/Project-Euler/Problem025.js +++ b/Project-Euler/Problem025.js @@ -39,9 +39,9 @@ function fibonacci(n) { else { let series = [0,1] for (let i = 2; i <= n; i++) { - series.push(sequence[i-1]+sequence[i-2]) + series.push(series[i-1]+series[i-2]) } - return sequence[n] + return series[n] } } From c674390a51843d8cfe69808d21638ef348e001a6 Mon Sep 17 00:00:00 2001 From: Adhiraj Date: 2021年10月17日 17:10:58 +0530 Subject: [PATCH 4/9] rewrote to improve runtime --- Project-Euler/Problem025.js | 34 ++++++++++------------------------ 1 file changed, 10 insertions(+), 24 deletions(-) diff --git a/Project-Euler/Problem025.js b/Project-Euler/Problem025.js index 048d43ddd4..5be7f19b42 100644 --- a/Project-Euler/Problem025.js +++ b/Project-Euler/Problem025.js @@ -28,32 +28,18 @@ // brute force method -function fibonacci(n) { - // Creates an array of Fibonacci numbers using the Fibonacci formula. Returns the nth element of the array. - if (n === 1) { - return 0 - } - else if (n === 2) { - return 1 - } - else { - let series = [0,1] - for (let i = 2; i <= n; i++) { - series.push(series[i-1]+series[i-2]) - } - return series[n] - } -} - -function fibonacciIndex(n = 1000) { - // Computes incrementing Fibonacci numbers starting from 3 and checks if its length is equal to n. Returns the term of the sequence in which it happens first. - let digits = 0 - let index = 2 - while (digits < n) { +function fibonacciIndex(t = 1000) { + let digits = 10n**BigInt(t-1) + let fib0 = BigInt(0) + let fib1 = BigInt(1) + let index = 1 + while (fib1 < digits) { // using this to compare number of digits instead of .toString() significantly improved run time + const tempfib = fib1 + fib1 = fib1 + fib0 + fib0 = tempfib index += 1 - digits = fibonacci(index).toString().length } - return index + return(index) } export { fibonacciIndex } From 64d0e55445033be4246627d47d303864d66abdf4 Mon Sep 17 00:00:00 2001 From: Adhiraj Date: 2021年10月18日 09:38:41 +0530 Subject: [PATCH 5/9] Update Problem025.js --- Project-Euler/Problem025.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Project-Euler/Problem025.js b/Project-Euler/Problem025.js index 5be7f19b42..f533d2a784 100644 --- a/Project-Euler/Problem025.js +++ b/Project-Euler/Problem025.js @@ -29,10 +29,10 @@ // brute force method function fibonacciIndex(t = 1000) { - let digits = 10n**BigInt(t-1) - let fib0 = BigInt(0) - let fib1 = BigInt(1) - let index = 1 + let digits = 10n**BigInt(t-1), + fib0 = BigInt(0), + fib1 = BigInt(1), + index = 1 while (fib1 < digits) { // using this to compare number of digits instead of .toString() significantly improved run time const tempfib = fib1 fib1 = fib1 + fib0 From 802bc79bceae574af93bca35a154dbd0c058028a Mon Sep 17 00:00:00 2001 From: Adhiraj Date: 2021年10月18日 20:59:40 +0530 Subject: [PATCH 6/9] fixed typo --- Project-Euler/Problem025.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project-Euler/Problem025.js b/Project-Euler/Problem025.js index f533d2a784..236ef90e63 100644 --- a/Project-Euler/Problem025.js +++ b/Project-Euler/Problem025.js @@ -1,5 +1,5 @@ /** -* Problem 20 - 1000-digit Fibonacci number +* Problem 25 - 1000-digit Fibonacci number * * @see {@link https://projecteuler.net/problem=25} * From d4ce5bc66f10304ba3d8a6699af8851eb5a1dcd5 Mon Sep 17 00:00:00 2001 From: Adhiraj Date: 2021年10月21日 17:44:00 +0530 Subject: [PATCH 7/9] added test file for Project-Euler/Problem025.js --- Project-Euler/test/Problem025.test.js | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 Project-Euler/test/Problem025.test.js diff --git a/Project-Euler/test/Problem025.test.js b/Project-Euler/test/Problem025.test.js new file mode 100644 index 0000000000..a5949fb645 --- /dev/null +++ b/Project-Euler/test/Problem025.test.js @@ -0,0 +1,27 @@ +import { fibonacciIndex } from '.../Problem025' + +describe ('Check Problem 25 - 1000 digit Fibonnaci number', () => { + it('First term of the Fibonnaci sequence containing 3 digits', () => { + expect(digitFibonacci(3)).toBe(12) + }) + + it('First term of the Fibonnaci sequence containing 10 digits', () => { + expect(digitFibonacci(10)).toBe(45) + }) + + it('First term of the Fibonnaci sequence containing 50 digits', () => { + expect(digitFibonacci(50)).toBe(237) + }) + + it('First term of the Fibonnaci sequence containing 100 digits', () => { + expect(digitFibonacci(100)).toBe(476) + }) + + it('First term of the Fibonnaci sequence containing 1000 digits', () => { + expect(digitFibonacci(1000)).toBe(4782) + }) + + it('First term of the Fibonnaci sequence containing 10000 digits', () => { + expect(digitFibonacci(10000)).toBe(47847) + }) +}) \ No newline at end of file From d19d160fdd34b301762bcdebba91288512faea8b Mon Sep 17 00:00:00 2001 From: Adhiraj Date: 2021年10月21日 17:53:02 +0530 Subject: [PATCH 8/9] Update Project-Euler/test/Problem025.test.js Co-authored-by: Rak Laptudirm --- Project-Euler/test/Problem025.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project-Euler/test/Problem025.test.js b/Project-Euler/test/Problem025.test.js index a5949fb645..b8090b7898 100644 --- a/Project-Euler/test/Problem025.test.js +++ b/Project-Euler/test/Problem025.test.js @@ -1,4 +1,4 @@ -import { fibonacciIndex } from '.../Problem025' +import { fibonacciIndex } from '../Problem025' describe ('Check Problem 25 - 1000 digit Fibonnaci number', () => { it('First term of the Fibonnaci sequence containing 3 digits', () => { From 16692cdde5b5f950a3440cad36471edd1d5641b3 Mon Sep 17 00:00:00 2001 From: Adhiraj Date: 2021年10月21日 18:18:32 +0530 Subject: [PATCH 9/9] Update Problem025.test.js --- Project-Euler/test/Problem025.test.js | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/Project-Euler/test/Problem025.test.js b/Project-Euler/test/Problem025.test.js index b8090b7898..e5b0627e1e 100644 --- a/Project-Euler/test/Problem025.test.js +++ b/Project-Euler/test/Problem025.test.js @@ -2,26 +2,26 @@ import { fibonacciIndex } from '../Problem025' describe ('Check Problem 25 - 1000 digit Fibonnaci number', () => { it('First term of the Fibonnaci sequence containing 3 digits', () => { - expect(digitFibonacci(3)).toBe(12) + expect(fibonacciIndex(3)).toBe(12) }) it('First term of the Fibonnaci sequence containing 10 digits', () => { - expect(digitFibonacci(10)).toBe(45) + expect(fibonacciIndex(10)).toBe(45) }) it('First term of the Fibonnaci sequence containing 50 digits', () => { - expect(digitFibonacci(50)).toBe(237) + expect(fibonacciIndex(50)).toBe(237) }) it('First term of the Fibonnaci sequence containing 100 digits', () => { - expect(digitFibonacci(100)).toBe(476) + expect(fibonacciIndex(100)).toBe(476) }) it('First term of the Fibonnaci sequence containing 1000 digits', () => { - expect(digitFibonacci(1000)).toBe(4782) + expect(fibonacciIndex(1000)).toBe(4782) }) it('First term of the Fibonnaci sequence containing 10000 digits', () => { - expect(digitFibonacci(10000)).toBe(47847) + expect(fibonacciIndex(10000)).toBe(47847) }) -}) \ No newline at end of file +})

AltStyle によって変換されたページ (->オリジナル) /