Last active
December 21, 2018 21:55
-
-
Save dmi3y/5259634 to your computer and use it in GitHub Desktop.
Fibonacci sequence (http://en.wikipedia.org/wiki/Fibonacci_number) sum http://jsbin.com/iqomew/2/edit, diversity tests http://jsperf.com/fibonacci-perfomance/5
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function fibonacciSum(n) { | |
'use strict'; | |
var | |
sequence = fibonacci(n); | |
return sequence.reduce(function(a, b) { | |
return a + b; | |
}); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function fibsum(n) { | |
var fib = [], | |
sum = 0, | |
cur, | |
prev = 0, | |
prevprev = 0; | |
for(; n--;) { | |
cur = prev + prevprev || 1; | |
prevprev = prev; | |
prev = cur; | |
fib.push(cur); | |
sum += cur; | |
} | |
// console.log(fib, sum); | |
return sum; | |
} | |
console.log(fibsum (10)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function fibonacci(n) { | |
'use strict'; | |
var | |
out = [1]; | |
function fibSeq(seq, i) { | |
var | |
item; | |
if ( i < n ) { | |
item = seq[i - 1] + seq[i - 2]; | |
seq.push(item); | |
fibSeq(seq, seq.length); | |
} | |
return seq; | |
} | |
if ( n >= 2 ) { | |
fibSeq([1,1], 2); | |
} else if ( n === 1 ) { | |
out = out.push(1); | |
} | |
return out; | |
} | |
function christmasPresents (days, current = 0, collect = [0]) { | |
if (current < days) { | |
collect.push(current + ) | |
christmasPresents(days, current + 1, collect) | |
} | |
return collect | |
} | |
var answer = christmasPresents(12) | |
console.log(answer) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment