Advertisement
kompilainenn

Untitled

Mar 14th, 2024
721
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.60 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 = set()
  6.         z = i
  7.         while True:
  8.             temp_2 = sum([int(x) ** 2 for x in str(z)])
  9.             if temp_2 in temp:
  10.                 bad_numbers.update(temp)
  11.                 break
  12.             elif temp_2 in bad_numbers:
  13.                 bad_numbers.update(temp)
  14.                 break
  15.             elif temp_2 == 1:
  16.                 result.append(i)
  17.                 break
  18.             elif temp_2 not in temp:
  19.                 temp.add(temp_2)
  20.                 z = temp_2
  21.     return result
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement