Skip to content

Instantly share code, notes, and snippets.

@cfredmond
Created March 4, 2022 22:12
Show Gist options
  • Select an option

  • Save cfredmond/b1c7f305ce1c90778e1c0ef6e01b5901 to your computer and use it in GitHub Desktop.

Select an option

Save cfredmond/b1c7f305ce1c90778e1c0ef6e01b5901 to your computer and use it in GitHub Desktop.
happy numbers
def foo(param):
cparam = param
happy = False
sums = []
while True:
l = [int(d)**2 for d in str(param)]
s = sum(l)
if s == 1:
happy = True
break
elif s in sums: # while testing i noticed that unhappy numbers created a list of repeating sumed elements. breaking out of loop if number appears
break
sums.append(s)
param = s
return { cparam : happy }
print([foo(e) for e in range(100)])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment