Skip to content

Instantly share code, notes, and snippets.

@VHarisop
Forked from mpetyx/Hardy-RamanujanNumber
Last active August 29, 2015 14:07
Show Gist options
  • Save VHarisop/09c2321c7d7ed8339540 to your computer and use it in GitHub Desktop.
Save VHarisop/09c2321c7d7ed8339540 to your computer and use it in GitHub Desktop.
__author__ = 'VHarisop'
''' original idea from mpetyx <github.com/mpetyx>
Generates Hardy-Ramanujan Numbers up to 250000 in under 1 sec.
'''
import sys
rng = int(sys.argv[1])
hrs = [0] * (2 * rng ** 3 + 1)
for x in range(1, rng):
for y in range(x, rng):
hrs[x**3 + y**3] += 1
for i in range(0, 2 * rng**3 + 1):
if hrs[i] > 1:
print i
@VHarisop
Copy link
Author

VHarisop commented Oct 2, 2014

Initially (as can be seen in the revision history), I was using a class with the sum as key, and a set to contain the possible number tuples that generate it, with the intention of putting those in a, indeed sparse, array. But I was exploring what then became this version so I never tried out your way, which is pretty cool.

I am pretty familiar with B/B+ trees (and SWIG as well!), so if I find some spare time I might look into this solution. I don't think there is a mathematical property that holds for these numbers, but - concerning your method - there might be a way to generate them (at least) in a partially sorted order to make the most out of Python's Timsort.

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