Skip to content

Commit

Permalink
fibonacci defaults to output Numbers instead of String
Browse files Browse the repository at this point in the history
  • Loading branch information
tmhglnd committed Sep 9, 2020
1 parent 4bfff02 commit 6edd3cd
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 20 deletions.
56 changes: 45 additions & 11 deletions src/gen-complex.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,9 @@ exports.linden = linden;
// integer sequences: fibonacci, pell, tribonacci
//
// @param {Int} -> output length of array
// @param {Int} -> multiplier t
// @param {Int} -> start value 1
// @param {Int} -> start value 2
// @param {Int} -> multiplier t
// @return {Array} -> array of BigNumber objects
//
function numBonacci(len=1, s1=0, s2=1, t=1){
Expand All @@ -163,25 +163,35 @@ function numBonacci(len=1, s1=0, s2=1, t=1){
}

// Generate any n-bonacci sequence as an array of BigNumber objects
// for export fuction
// for export fuction. F(n) = t * F(n-1) + F(n-2)
//
// @param {Int} -> output length of array
// @param {Int} -> start value 1 (optional, default=0)
// @param {Int} -> start value 2 (optional, default=1)
// @param {Int} -> multiplier (optional, default=1)
// @return {String-Array} -> array of bignumbers as strings
//
function nbonacci(len=1, s1=0, s2=1, t=1){
return numBonacci(len, s1, s2, t).map(x => x.toFixed());
return numBonacci(len, s1, s2, t).map(x => x.toNumber());
}
exports.nbonacci = nbonacci;

// Generate the Fibonacci sequence as an array of BigNumber objects
// F(n) = F(n-1) + F(n-2). The ratio between consecutive numbers in
// the fibonacci sequence tends towards the Golden Ratio (1+√5)/2
// OEIS: A000045 (Online Encyclopedia of Integer Sequences)
// When working with larger fibonacci-numbers then possible in 64-bit
// Set the toString to true
//
// @param {Int} -> output length of array
// @param {Int} -> offset in sequence (optional, default=0)
// @param {Bool} -> numbers as strings (optional, default=false)
// @return {String-Array} -> array of bignumbers as strings
//
function fibonacci(len=1, offset=0){
var f = numBonacci(len+offset, 0, 1, 1).map(x => x.toFixed())
function fibonacci(len=1, offset=0, toString=false){
var f = numBonacci(len+offset, 0, 1, 1).map(x => {
return (toString)? x.toFixed() : x.toNumber()
});
if (offset > 0){
return f.slice(offset, offset+len);
}
Expand Down Expand Up @@ -242,10 +252,18 @@ function pisanoPeriod(mod=2, length=64){
// OEIS: A006190 (Online Encyclopedia of Integer Sequences)
//
// @param {Int} -> output length of array
// @param {Int} -> offset in sequence (optional, default=0)
// @param {Bool} -> numbers as strings (optional, default=false)
// @return {String-Array} -> array of bignumbers as strings
//
function pell(len=1){
return numBonacci(len, 0, 1, 2).map(x => x.toFixed());
function pell(len=1, offset=0, toString=false){
var f = numBonacci(len+offset, 0, 1, 2).map(x => {
return (toString)? x.toFixed() : x.toNumber()
});
if (offset > 0){
return f.slice(offset, offset+len);
}
return f;
}
exports.pell = pell;

Expand All @@ -255,10 +273,18 @@ exports.pell = pell;
// OEIS: A000129 (Online Encyclopedia of Integer Sequences)
//
// @param {Int} -> output length of array
// @param {Int} -> offset in sequence (optional, default=0)
// @param {Bool} -> numbers as strings (optional, default=false)
// @return {String-Array} -> array of bignumbers as strings
//
function threeFibonacci(len=1){
return numBonacci(len, 0, 1, 3).map(x => x.toFixed());
function threeFibonacci(len=1, offset=0, toString=false){
let f = numBonacci(len+offset, 0, 1, 3).map(x => {
return (toString)? x.toFixed() : x.toNumber()
});
if (offset > 0){
return f.slice(offset, offset+len);
}
return f;
}
exports.threeFibonacci = threeFibonacci;

Expand All @@ -267,9 +293,17 @@ exports.threeFibonacci = threeFibonacci;
// OEIS: A000032 (Online Encyclopedia of Integer Sequences)
//
// @param {Int} -> output length of array
// @param {Int} -> offset in sequence (optional, default=0)
// @param {Bool} -> numbers as strings (optional, default=false)
// @return {String-Array} -> array of bignumbers as strings
//
function lucas(len=1){
return numBonacci(len, 2, 1, 1).map(x => x.toFixed());
function lucas(len=1, offset=0, toString=false){
let f = numBonacci(len+offset, 2, 1, 1).map(x => {
return (toString)? x.toFixed() : x.toNumber()
});
if (offset > 0){
return f.slice(offset, offset+len);
}
return f;
}
exports.lucas = lucas;
19 changes: 10 additions & 9 deletions test/serialism.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,14 @@ fullTest();

function fullTest(){
console.time('Total Time');
testSerial();
testGen();
// testSerial();
// testGen();
testAlgo();
testRand();
testMod();
testStat();
testTranslate();
testUtil();
// testRand();
// testMod();
// testStat();
// testTranslate();
// testUtil();

pagebreak("Test Passed");
console.timeEnd('Total Time');
Expand Down Expand Up @@ -172,8 +172,9 @@ function testAlgo(){
pagebreak("Fibonacci");
test('Algo.fibonacci()');
test("Algo.fibonacci(12)");
test("Algo.fibonacci(2, 100)");
test('Algo.fibonacci(1, 100)[0].split("").map(x => Number(x))');
// test("Algo.fibonacci(120)");
// test("Algo.fibonacci(2, 100)");
// test('Algo.fibonacci(1, 100).toString().split("").map(x => Number(x))');

test('Algo.pisano()');
test("Algo.pisano(12)");
Expand Down

0 comments on commit 6edd3cd

Please sign in to comment.