In [1]:
def factorize(x):
    if x <= 1 or divmod(x, 1)[1] != 0:
        raise ValueError
    factors = []
    f = 2
    while f * f <= x:
        if x % f == 0:
            factors.append(f)
            x //= f
        else:
            f += 1
    if x > 1:
        factors.append(x)
    return factors

In [2]:
n = 52
factorize(n)


Out[2]:
[2, 2, 13]

In [3]:
known_good_output = factorize(n)

In [4]:
def largest_factor(x):
    return factorize(x)[-1]

In [5]:
largest_factor(n)


Out[5]:
13

In [6]:
largest_factor(int('1'*4))


Out[6]:
101

In [7]:
def test_largest_factor_52():
    assert(largest_factor(52) == 13)

In [8]:
n = 52
%timeit factorize(n)


The slowest run took 6.63 times longer than the fastest. This could mean that an intermediate result is being cached 
100000 loops, best of 3: 2.12 µs per loop

In [9]:
def factorize(x):
    if x <= 1 or divmod(x, 1)[1] != 0:
        raise ValueError
    factors = []
    f = 2
    while f * f <= x:
        if x % f == 0:
            factors.append(f)
            x //= f
        else:
            if f > 2:
                f += 2
            else:
                f = 3
    if x > 1:
        factors.append(x)
    return factors

In [10]:
n = 52
assert known_good_output == factorize(n)
%timeit factorize(n)


100000 loops, best of 3: 2.15 µs per loop

In [11]:
1.25 % 1, -1.25 % 1, 2 % 1


Out[11]:
(0.25, 0.75, 0)

In [12]:
import math

def foo(n):
    %timeit int(n) != n
    %timeit n % 1 != 0
    %timeit math.modf(n)[0] != 0
    %timeit divmod(n, 1)[1] != 0
    %timeit n != math.trunc(n)
    %timeit n != math.floor(n)
    %timeit n != math.ceil(n)

In [13]:
foo(1.5)


The slowest run took 37.36 times longer than the fastest. This could mean that an intermediate result is being cached 
1000000 loops, best of 3: 211 ns per loop
The slowest run took 54.40 times longer than the fastest. This could mean that an intermediate result is being cached 
10000000 loops, best of 3: 92 ns per loop
The slowest run took 24.29 times longer than the fastest. This could mean that an intermediate result is being cached 
1000000 loops, best of 3: 324 ns per loop
The slowest run took 23.97 times longer than the fastest. This could mean that an intermediate result is being cached 
1000000 loops, best of 3: 298 ns per loop
The slowest run took 24.52 times longer than the fastest. This could mean that an intermediate result is being cached 
1000000 loops, best of 3: 292 ns per loop
The slowest run took 30.24 times longer than the fastest. This could mean that an intermediate result is being cached 
1000000 loops, best of 3: 229 ns per loop
The slowest run took 68.74 times longer than the fastest. This could mean that an intermediate result is being cached 
1000000 loops, best of 3: 232 ns per loop

In [14]:
foo(1234)


The slowest run took 23.66 times longer than the fastest. This could mean that an intermediate result is being cached 
1000000 loops, best of 3: 212 ns per loop
The slowest run took 45.33 times longer than the fastest. This could mean that an intermediate result is being cached 
10000000 loops, best of 3: 89.4 ns per loop
The slowest run took 25.27 times longer than the fastest. This could mean that an intermediate result is being cached 
1000000 loops, best of 3: 321 ns per loop
The slowest run took 23.80 times longer than the fastest. This could mean that an intermediate result is being cached 
1000000 loops, best of 3: 301 ns per loop
The slowest run took 23.95 times longer than the fastest. This could mean that an intermediate result is being cached 
1000000 loops, best of 3: 289 ns per loop
The slowest run took 34.29 times longer than the fastest. This could mean that an intermediate result is being cached 
1000000 loops, best of 3: 229 ns per loop
The slowest run took 34.20 times longer than the fastest. This could mean that an intermediate result is being cached 
1000000 loops, best of 3: 230 ns per loop

Of the above, n != int(n) is the most readable. It's not terribly slow, so it is used below.


In [15]:
def factorize(x):
    if x <= 1 or x != int(x):
        raise ValueError
    factors = []
    f = 2
    while f * f <= x:
        if x % f == 0:
            factors.append(f)
            x //= f
        else:
            if f > 2:
                f += 2
            else:
                f = 3
    if x > 1:
        factors.append(x)
    return factors

In [16]:
n = 52
assert known_good_output == factorize(n)
%timeit factorize(n)


1000000 loops, best of 3: 1.96 µs per loop

In [17]:
def factorize(x):
    if x <= 1 or x != int(x):
        raise ValueError
    factors = []
    f = 2
    while x > 1:
        if x % f == 0:
            factors.append(f)
            x //= f
        else:
            f += 1
    return factors

In [18]:
n = 52
assert known_good_output == factorize(n)
%timeit factorize(n)


100000 loops, best of 3: 2.83 µs per loop

In [19]:
def factorize(x):
    if x <= 1 or x != int(x):
        raise ValueError
    factors = []
    f = 2
    while x > 1:
        if x % f == 0:
            factors.append(f)
            x //= f
        else:
            if f == 2:
                f = 3
            else:
                f += 2
    return factors

In [20]:
n = 52
assert known_good_output == factorize(n)
%timeit factorize(n)


100000 loops, best of 3: 2.53 µs per loop

In [21]:
def factorize(x):
    if x <= 1 or x != int(x):
        raise ValueError
    factors = []
    f = 2
    while x > 1:
        while x % f == 0:
            factors.append(f)
            x //= f
        else:
            if f == 2:
                f = 3
            else:
                f += 2
    return factors

In [22]:
n = 52
assert known_good_output == factorize(n)
%timeit factorize(n)


The slowest run took 5.34 times longer than the fastest. This could mean that an intermediate result is being cached 
100000 loops, best of 3: 2.99 µs per loop