Advertisement
kompilainenn

Untitled

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