Created
September 4, 2016 05:14
-
-
Save SimplGy/e7a8dee722b5e9b379810db473bddefa to your computer and use it in GitHub Desktop.
A beautiful piece of code I didn't write
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
// https://programmingpraxis.com/2011/08/30/hamming-numbers/ | |
// cosmin said | |
// October 30, 2012 at 7:14 PM | |
// Another solution based upon Dijkstra’s paper: | |
def hammingSeq(N): | |
h = [1] | |
i2, i3, i5 = 0, 0, 0 | |
for i in xrange(1, N): | |
x = min(2*h[i2], 3*h[i3], 5*h[i5]) | |
h.append(x) | |
if 2*h[i2] <= x: i2 += 1 | |
if 3*h[i3] <= x: i3 += 1 | |
if 5*h[i5] <= x: i5 += 1 | |
return h | |
print hammingSeq(1000) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I think 12-14 can just be
==
instead of<=