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

+ from math import sqrt, ceil
+ 
+ 
  def divisor_generator(n):
-     big_factors = {1}
+     yield 1
-     for i in range(2, n):
+     for i in range(2, ceil(sqrt(n))):
-         if i in big_factors:
-             break
          if n % i == 0:
-             big_factors.add(n // i)
              yield i
-     yield from big_factors
+             yield n // i
  
  
  def is_perfect(n):
      return n == sum(divisor_generator(n))
  
Refactor to give faster solution
## Code Before:
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))

## Instruction:
Refactor to give faster solution
## Code After:
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))
