Advertisement
kompilainenn

Untitled

Mar 14th, 2024
572
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.58 KB | None | 0 0
  1. def perf_happy(n):
  2.     result = []
  3.     bad_numbers = set()
  4.     for i in range(1, n + 1):
  5.         temp = []
  6.         z = i
  7.         while True:
  8.             temp_2 = sum([int(x) ** 2 for x in str(z)])
  9.             if temp_2 in bad_numbers:
  10.                 break
  11.             elif temp_2 in temp:
  12.                 bad_numbers.update(set(temp))
  13.                 break
  14.             elif temp_2 == 1:
  15.                 result.append(i)
  16.                 break
  17.             elif temp_2 not in temp:
  18.                 temp.append(temp_2)
  19.                 z = temp_2
  20.            
  21.     return result
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement