Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Вернуть n-ое число из последовательности Фибоначчи (0 1 1 2 3 5 8 ...)
- */
- function fib(n) {
- let results = [0,1];
- function find(n) {
- if ( n == 0 ) return results[0];
- if (n == 1) return results[1];
- if (!results[n]){
- results[n] = find(n-2) + find(n-1);
- }
- return results[n];
- }
- return find(n);
- }
- console.log(fib(6)) //8
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement