Created
September 12, 2016 08:23
-
-
Save timhuff/efb09817bda1b76d5223ceaf820c293d to your computer and use it in GitHub Desktop.
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
# set l to Math.log because you use it twice | |
l = Math.log | |
# create a recursive function, r, to iterate on a chain until it reaches a fixed point | |
# n is the input for the current iteration | |
r = (n)-> | |
### | |
This next line is a mouthful. "l(n)/l 2" computes the log base 2 of n. This is the index of the highest set bit in n | |
The reduce function is basically a sum across the bits of the number. | |
It adds 3 to the sum if the bit is set ("one".length) and 4 if the bit is not set ("zero".length) | |
So, in a nutshell, z is set to the number of letters in the binary phrase for the number n | |
### | |
z = [0..l(n)/l 2].reduce((a,b)->`1<<b&n?3+a:4+a`),0) | |
# if a fixed point has been reached, return it. else, call this function on z | |
`z==n?n:r(z)` | |
# create hash | |
c = {} | |
# for 1 to 1 million... | |
[1..1000000].map (n)-> | |
# use the r function to find the fixed point. | |
# initialize the c hash value for that key to zero | |
c[r n]?=0 | |
# increment value | |
c[r n]++ | |
# output hash | |
c |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment