Ad

Faster than previsous versions by avoiding sorting and insertion of factor lists. Style fix (not is not a function)

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