Created
January 2, 2018 14:15
-
-
Save lhuria94/0a994f1048b046cfa1310a6e826ada75 to your computer and use it in GitHub Desktop.
If you have completed the Tribonacci sequence kata, you would know by now that mister Fibonacci has at least a bigger brother. If not, give it a quick look to get how things work.
This file contains hidden or 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
// Well, time to expand the family a little more: think of a Quadribonacci starting with a signature of 4 elements and each following element is the sum of the 4 previous, a Pentabonacci (well Cinquebonacci would probably sound a bit more italian, but it would also sound really awful) with a signature of 5 elements and each following element is the sum of the 5 previous, and so on. | |
// Well, guess what? You have to build a Xbonacci function that takes a signature of X elements - and remember each next element is the sum of the last X elements - and returns the first n elements of the so seeded sequence. | |
// xbonacci {1,1,1,1} 10 = {1,1,1,1,4,7,13,25,49,94} | |
// xbonacci {0,0,0,0,1} 10 = {0,0,0,0,1,1,2,4,8,16} | |
// xbonacci {1,0,0,0,0,0,1} 10 = {1,0,0,0,0,0,1,2,3,6} | |
// xbonacci {1,1} produces the Fibonacci sequence | |
function Xbonacci(signature, n){ | |
var tempArr = signature.slice(0); | |
const reducer = (accumulator, currentValue) => accumulator + currentValue; | |
// Looping through. | |
for (var i=1; i<=n; i++) { | |
// Get Last n elements. | |
var lastnEl = tempArr.slice(-signature.length); | |
// Pushing it to new arr. | |
tempArr.push(lastnEl.reduce(reducer)); | |
} | |
return tempArr.slice(0, n); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment