Ad

Refactored to insert in order. This may run a bit faster due to no longer using sort() function.

Code
Diff
  • def divisors(n):
        fact = []
        for i in range(1, int(n**0.5) + 1):
            if not(n % i):
                v = n // i
                if v != i:
                    fact.insert(len(fact)//2,v)
                fact.insert(len(fact)//2,i)
        return fact
    • def divisors(n):
    • fact = [];
    • fact = []
    • for i in range(1, int(n**0.5) + 1):
    • if not(n % i):
    • fact.append(i)
    • if n / i != i:
    • fact.append(n / i)
    • fact.sort()
    • v = n // i
    • if v != i:
    • fact.insert(len(fact)//2,v)
    • fact.insert(len(fact)//2,i)
    • return fact