Skip to content

Instantly share code, notes, and snippets.

@arpit-omprakash
Last active September 16, 2019 15:27
Show Gist options
  • Save arpit-omprakash/32ba55b8a9746a90122f043f54121929 to your computer and use it in GitHub Desktop.
Save arpit-omprakash/32ba55b8a9746a90122f043f54121929 to your computer and use it in GitHub Desktop.
Happy numbers question from Project Euler
# Necessary imports
from math import pow
# List to store the values
happylist = []
# Function to check if the number 'n' ia a happy number
def is_happy(n):
  m = int(n)
  if m == 4:
    return 0
  elif m == 1:
    return 1
  else:
    l = [int(j) for j in str(m)]
    new = 0
    for k in l:
      new += int(pow(k,2))
      return is_happy(new)
# Loop to check numbers from 1 to 1000 and add them to list
for i in range(1,1000):
  ans = is_happy(i)
  if ans == 1:
  happylist.append(i)
# Printing the results
print happylist
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment