Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- export function fibo(n) {
- if (n<1) return undefined;
- arr=fiboarray(n);
- return arr[n];
- }
- export function fibosomma(n) {
- if (n<1) return undefined;
- arr=fiboarray(n);
- let somma=0;
- for(let i in arr) { somma += arr[i]; }
- //Soluzione ES6: somma=arr.reduce((a, b) => a + b, 0);
- return somma;
- }
- export function fiboarray(n) {
- if (n<1) return undefined;
- let fib = [];
- fib[0] = 0;
- fib[1] = 1;
- for (let i = 2; i <= n; i++) {
- fib[i] = fib[i - 2] + fib[i - 1];
- }
- return fib;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement