Advertisement
kompilainenn

Untitled

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