2.4 KiB
2.4 KiB
id, challengeType, title, forumTopicId, localeTitle
id | challengeType | title | forumTopicId | localeTitle |
---|---|---|---|---|
5900f3851000cf542c50fe98 | 5 | Problem 25: 1000-digit Fibonacci number | 301897 | Задача 25: 1000-значный номер Фибоначчи |
Description
F n = F n-1 + F n-2 , где F 1 = 1 и F 2 = 1.
Следовательно, первые 12 терминов будут: F 1 = 1
F 2 = 1
F 3 = 2
F 4 = 3
F 5 = 5
F 6 = 8
F 7 = 13
F 8 = 21
F 9 = 34
F 10 = 55
F 11 = 89
F 12 = 144
Instructions
Tests
tests:
- text: <code>digitFibonacci(5)</code> should return 21.
testString: assert.strictEqual(digitFibonacci(5), 21);
- text: <code>digitFibonacci(10)</code> should return 45.
testString: assert.strictEqual(digitFibonacci(10), 45);
- text: <code>digitFibonacci(15)</code> should return 69.
testString: assert.strictEqual(digitFibonacci(15), 69);
- text: <code>digitFibonacci(20)</code> should return 93.
testString: assert.strictEqual(digitFibonacci(20), 93);
Challenge Seed
function digitFibonacci(n) {
// Good luck!
return n;
}
digitFibonacci(20);
Solution
const digitFibonacci = (n) => {
const digits = (num) => {
return num.toString().length;
};
let f1 = 1;
let f2 = 1;
let index = 3;
while (true) {
let fn = f1 + f2;
if (digits(fn) === n) return index;
[f1, f2] = [f2, fn];
index++;
}
};