Skip to content

Instantly share code, notes, and snippets.

@sushain97
Last active August 29, 2015 14:00
Show Gist options
  • Save sushain97/11185841 to your computer and use it in GitHub Desktop.
Save sushain97/11185841 to your computer and use it in GitHub Desktop.
import sys
limit = 5 if not len(sys.argv) > 1 else int(sys.argv[1])
found = {}
toler = 10 ** -5
energy = 1
while not set(found.keys()) >= set(range(1, limit + 1)):
states = []
for nx in range(1, int(energy ** .5) + 1):
ny = (energy - nx ** 2) ** .5
if abs(int(ny) - ny) < toler and not ny < toler:
states.append((nx, int(ny)))
nstates = len(states)
if nstates not in found and nstates is not 0:
found[nstates] = states
print('%5.2f%% %s: %s - %s' % (float(len(set(found.keys()) & set(range(1, limit)))) / limit * 100, nstates, energy, states))
energy += 1
print('\n%s' % found)
'''
from collections import defaultdict
from itertools import product
states = defaultdict(list)
minstates = {}
for nx, ny in product(range(1, limit), range(1, limit)):
states[nx ** 2 + ny ** 2].append((nx, ny))
for state in range(1, min(nx ** 2, ny ** 2)):
n = len(states[state])
if n not in minstates or state < minstates[n]:
minstates[n] = state
del states[state]
print(minstates)
'''
'''
states = sorted(states.items(), key=lambda t: (len(t[1]), t[0]))
found = []
for state in states:
n = len(state[1])
if n not in found:
print('%s - %s: %s' % (n, state[0], state[1]))
found.append(n)
'''
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment