Skip to content

Commit

Permalink
updated bignumber collatz mod
Browse files Browse the repository at this point in the history
  • Loading branch information
tmhglnd committed Mar 17, 2021
1 parent ac0252c commit f8b8f45
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 9 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -74,6 +76,8 @@ scl.names;
// '08-19', ... and 5000 more]
```

## Expand and stretch array

Expand an array by analyzing its internal structure

```js
Expand Down
26 changes: 23 additions & 3 deletions docs/algorithmic-methods.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand All @@ -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
Expand Down
25 changes: 22 additions & 3 deletions src/gen-complex.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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
Expand Down
8 changes: 5 additions & 3 deletions test/serialism.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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()');
Expand Down

0 comments on commit f8b8f45

Please sign in to comment.