def divisor_generator(n):
    big_factors = {1}
    for i in range(2, n):
        if i in big_factors:
            break
        if n % i == 0:
            big_factors.add(n // i)
            yield i
    yield from big_factors


def is_perfect(n):
    return n == sum(divisor_generator(n))

from math import sqrt, ceil


def divisor_generator(n):
    yield 1
    for i in range(2, ceil(sqrt(n))):
        if n % i == 0:
            yield i
            yield n // i


def is_perfect(n):
    return n == sum(divisor_generator(n))

Refactor to give faster solution
Refactor to give faster solution
