Skip to content

Instantly share code, notes, and snippets.

@robertvunabandi
Created June 29, 2017 06:01
Show Gist options
  • Save robertvunabandi/3cc6432cc672f1e2576c3f76ec5a6a16 to your computer and use it in GitHub Desktop.
Save robertvunabandi/3cc6432cc672f1e2576c3f76ec5a6a16 to your computer and use it in GitHub Desktop.
Project Euler #31
def Tree3(s, p, c, L):
# attempts the same as &Tree and &Tree2 with a different approach
# this assumes that p is ordered from smallest to highest
res = 0
lfinal = set()
for i in range(len(p)):
pos = [L]
tSum = c
while tSum < s:
pos.append(pos[-1]+[p[i]])
tSum += p[i]
for j in pos:
jSum = sum(j)
if jSum > s: continue
elif jSum == s:
res += 1
lfinal.add(n_from_l(j))
else:
n = Tree3(s, p[i+1:], jSum, j)
res += n[0]
for k in sorted(n[1]): lfinal.add(k)
return len(lfinal), lfinal
# ans = Tree3(200, [1, 2, 5, 10, 20, 50, 100, 200], 0, []) # at least 5 min of waiting
# print(ans[0])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment