A (lazy but slow) Python solution which hasn't even yielded 33550336 in 5 minutes, despite a subtle optimization allowing to find divisors in O(√n)… Probably could use some optimization to prevent trying all possible integers. :-)
O(√n)
def find_divisors(n: int): divisors = [] m = 1 while m * m < n: if n % m == 0: yield m if m != 1: yield n / m m += 1 def is_perfect(n: int) -> bool: return sum(find_divisors(n)) == n if __name__ == '__main__': from itertools import cycle all_integers = cycle() for n in filter(is_perfect, all_integers): print(n)
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
A (lazy but slow) Python solution which hasn't even yielded 33550336 in 5 minutes, despite a subtle optimization allowing to find divisors in
O(√n)
… Probably could use some optimization to prevent trying all possible integers. :-)