Advertisement
kompilainenn

Untitled

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