diff --git a/README.md b/README.md index 48bca905..1f93aef0 100644 --- a/README.md +++ b/README.md @@ -51,6 +51,8 @@ Util.draw(gens); // █ █ █ ████ █ █ ██ █ ██ █ █ █ ██ ``` +## Scale tuning + Use the `new TL.Scala()` class to import a *.scl* file (Scala tuning format) to work with custom tuning systems apart from the Western 12-TET (Equal Temperament) tuning or use one of the tunings from a database with over 5000 tunings from [Stichting Huygens-Fokker](http://www.huygens-fokker.org/scala/). ```js @@ -74,6 +76,8 @@ scl.names; // '08-19', ... and 5000 more] ``` +## Expand and stretch array + Expand an array by analyzing its internal structure ```js diff --git a/docs/algorithmic-methods.md b/docs/algorithmic-methods.md index 350f6b10..f9d93193 100644 --- a/docs/algorithmic-methods.md +++ b/docs/algorithmic-methods.md @@ -179,7 +179,7 @@ ca.rule(9); ``` -## collatz +## collatz conjecture Generate an array of numbers from the Collatz Conjecture, also known as the `3n+1` conjecture. Start with any positive integer `n`. Each next number is obtained from the previous number as follows: If the previous number is even then the next term is the previous term divided by 2. If the previous term is odd tthen the next term is 3 times the prevous term plus 1. The conjecture is that no matter what value of `n`, the sequence will always reach one. The length of the output is quite unpredicatable and can therefore be an interesting sequence for algorithmic composition. @@ -188,8 +188,28 @@ Generate an array of numbers from the Collatz Conjecture, also known as the `3n+ ```js // the collatz sequence for the number 15 -Algo.collatz(15); -//=> [] +Algo.collatz(7); +//=> [ +// 1, 2, 4, 8, 16, 5, +// 10, 20, 40, 13, 26, 52, +// 17, 34, 11, 22 +// ] + +// return the collatz sequence with a modulus operation (default = 2) +Algo.collatzMod(7, 12); +//=> [ +// 1, 2, 4, 8, 4, 5, +// 10, 8, 4, 1, 2, 4, +// 5, 10, 11, 10 +// ] + +// the collatz sequence can encounter quite big values +// so alternatively you can use bigCollatz and bigCollatzMod +// to allow for larger number calculations +Algo.bigCollatz('931386509544713451').length; +// => 2283 + +Algo.bigCollatzMod('931386509544713451'); ``` ## fibonacci diff --git a/src/gen-complex.js b/src/gen-complex.js index 175d995e..25053494 100644 --- a/src/gen-complex.js +++ b/src/gen-complex.js @@ -157,12 +157,18 @@ function collatz(n=12){ } exports.collatz = collatz; -function collatzMod(n=12){ - return Util.mod(collatz(n), 2); +// Return the modulus of a collatz conjecture sequence +// Set the modulo +// +// @param {Int+} -> starting number +// @param {Int+} -> modulus +// +function collatzMod(n=12, m=2){ + return Util.mod(collatz(n), Math.min(m, Math.floor(m))); } exports.collatzMod = collatzMod; -// The collatz conjecture with BigNumber +// The collatz conjecture with BigNumber library // function bigCollatz(n){ let num = new BigNumber(n); @@ -181,6 +187,19 @@ function bigCollatz(n){ } exports.bigCollatz = bigCollatz; +// Return the modulus of a collatz conjecture sequence +// Set the modulo +// +function bigCollatzMod(n=12, m=2){ + let arr = bigCollatz(n); + for (let i in arr){ + arr[i] = new BigNumber(arr[i]); + arr[i] = arr[i].mod(m).toNumber(); + } + return arr; +} +exports.bigCollatzMod = bigCollatzMod; + // Generate any n-bonacci sequence as an array of BigNumber objects // F(n) = t * F(n-1) + F(n-2). This possibly generatres various // integer sequences: fibonacci, pell, tribonacci diff --git a/test/serialism.test.js b/test/serialism.test.js index b551ffb1..42516bc8 100644 --- a/test/serialism.test.js +++ b/test/serialism.test.js @@ -55,12 +55,14 @@ console.log(); fullTest(); +// console.log(Algo.bigCollatzMod(100000)); + function fullTest(){ console.time('Total Time'); // testSerial(); // testGen(); - testAlgo(); + // testAlgo(); // testRand(); // testMod(); // testStat(); @@ -190,8 +192,8 @@ function testAlgo(){ test('Algo.collatzMod()'); test('Algo.collatzMod(43)'); - test('Algo.collatzMod(7)'); - test('Algo.collatzMod(314)'); + test('Algo.collatzMod(7, 12)'); + test('Algo.collatzMod(314, 5)'); pagebreak("Fibonacci"); test('Algo.fibonacci()');