Created
January 9, 2017 20:58
-
-
Save nem035/9bca1b80c934f062793a4c6bfaaebf15 to your computer and use it in GitHub Desktop.
(Memoized) code to print the Collatz Sequence for a given number. https://en.wikipedia.org/wiki/Collatz_conjecture
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 collatzSequence() { | |
const storage = new Map(); | |
function run(x) { | |
if (typeof x !== 'number') throw new TypeError('Argument must be a number'); | |
let head = storage.get(x) || new ListNode(x); | |
let curr = head; | |
while (curr.value !== 1) { | |
if (storage.has(curr.value)) { | |
return head; | |
} | |
storage.set(curr.value, curr); | |
curr.next = createNext(curr.value); | |
curr = curr.next; | |
} | |
storage.set(x, head); | |
return storage.get(x); | |
} | |
function createNext(value) { | |
return new ListNode(value % 2 === 0 | |
? value / 2 | |
: (3*value + 1)); | |
} | |
function stringify(seq) { | |
let output = ''; | |
while (seq) { | |
let str = seq.value; | |
if (seq.next) { | |
str += '->'; | |
} | |
output += str; | |
seq = seq.next; | |
} | |
return output; | |
} | |
function toString(x) { | |
return stringify(run(x)); | |
} | |
function print(x) { | |
console.log(toString(x)); | |
} | |
function printTree() { | |
for (const [num, list] of storage.entries()) { | |
console.log(`${num}: ${stringify(list)}`); | |
} | |
} | |
function printTreeSorted() { | |
const nums = [...storage.keys()].sort((a, b) => a - b); | |
const spacing = ' '.repeat(String(Math.max(...nums)).length - String(num).length + 1); | |
for (const num of nums) { | |
console.log(`${num}:${spacing}${stringify(storage.get(num))}`); | |
} | |
} | |
function ListNode(x) { | |
this.value = x; | |
this.next = null; | |
} | |
return { | |
print, | |
printTree, | |
printTreeSorted | |
}; | |
} | |
var seq = collatzSequence(); | |
seq.printTreeSorted(); // '' | |
seq.print(717); | |
console.log('\nCollatz Tree\n\n'); | |
seq.printTreeSorted(); | |
/* | |
OUTPUT: | |
717->2152->1076->538->269->808->404->202->101->304->152->76->38->19->58->29->88->44->22->11->34->17->52->26->13->40->20->10->5->16->8->4->2->1 | |
Collatz Tree | |
2: 2->1 | |
4: 4->2->1 | |
5: 5->16->8->4->2->1 | |
8: 8->4->2->1 | |
10: 10->5->16->8->4->2->1 | |
11: 11->34->17->52->26->13->40->20->10->5->16->8->4->2->1 | |
13: 13->40->20->10->5->16->8->4->2->1 | |
16: 16->8->4->2->1 | |
17: 17->52->26->13->40->20->10->5->16->8->4->2->1 | |
19: 19->58->29->88->44->22->11->34->17->52->26->13->40->20->10->5->16->8->4->2->1 | |
20: 20->10->5->16->8->4->2->1 | |
22: 22->11->34->17->52->26->13->40->20->10->5->16->8->4->2->1 | |
26: 26->13->40->20->10->5->16->8->4->2->1 | |
29: 29->88->44->22->11->34->17->52->26->13->40->20->10->5->16->8->4->2->1 | |
34: 34->17->52->26->13->40->20->10->5->16->8->4->2->1 | |
38: 38->19->58->29->88->44->22->11->34->17->52->26->13->40->20->10->5->16->8->4->2->1 | |
40: 40->20->10->5->16->8->4->2->1 | |
44: 44->22->11->34->17->52->26->13->40->20->10->5->16->8->4->2->1 | |
52: 52->26->13->40->20->10->5->16->8->4->2->1 | |
58: 58->29->88->44->22->11->34->17->52->26->13->40->20->10->5->16->8->4->2->1 | |
76: 76->38->19->58->29->88->44->22->11->34->17->52->26->13->40->20->10->5->16->8->4->2->1 | |
88: 88->44->22->11->34->17->52->26->13->40->20->10->5->16->8->4->2->1 | |
101: 101->304->152->76->38->19->58->29->88->44->22->11->34->17->52->26->13->40->20->10->5->16->8->4->2->1 | |
152: 152->76->38->19->58->29->88->44->22->11->34->17->52->26->13->40->20->10->5->16->8->4->2->1 | |
202: 202->101->304->152->76->38->19->58->29->88->44->22->11->34->17->52->26->13->40->20->10->5->16->8->4->2->1 | |
269: 269->808->404->202->101->304->152->76->38->19->58->29->88->44->22->11->34->17->52->26->13->40->20->10->5->16->8->4->2->1 | |
304: 304->152->76->38->19->58->29->88->44->22->11->34->17->52->26->13->40->20->10->5->16->8->4->2->1 | |
404: 404->202->101->304->152->76->38->19->58->29->88->44->22->11->34->17->52->26->13->40->20->10->5->16->8->4->2->1 | |
538: 538->269->808->404->202->101->304->152->76->38->19->58->29->88->44->22->11->34->17->52->26->13->40->20->10->5->16->8->4->2->1 | |
717: 717->2152->1076->538->269->808->404->202->101->304->152->76->38->19->58->29->88->44->22->11->34->17->52->26->13->40->20->10->5->16->8->4->2->1 | |
808: 808->404->202->101->304->152->76->38->19->58->29->88->44->22->11->34->17->52->26->13->40->20->10->5->16->8->4->2->1 | |
1076: 1076->538->269->808->404->202->101->304->152->76->38->19->58->29->88->44->22->11->34->17->52->26->13->40->20->10->5->16->8->4->2->1 | |
2152: 2152->1076->538->269->808->404->202->101->304->152->76->38->19->58->29->88->44->22->11->34->17->52->26->13->40->20->10->5->16->8->4->2->1 | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment