Skip to content

Instantly share code, notes, and snippets.

@SimplGy
Created September 4, 2016 05:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SimplGy/e7a8dee722b5e9b379810db473bddefa to your computer and use it in GitHub Desktop.
Save SimplGy/e7a8dee722b5e9b379810db473bddefa to your computer and use it in GitHub Desktop.
A beautiful piece of code I didn't write
// 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)
@SimplGy
Copy link
Author

SimplGy commented Sep 4, 2016

I think 12-14 can just be == instead of <=

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment