ramonsaraiva / timy

Minimalist measurement of python code time

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

prime factors example in readme

dcontant opened this issue · comments

commented

the example does not return the prime factors of n

try this instead

def prime_factors(n):
    with timy.Timer('Factors') as timer:
        i = 2
        factors = []
        def add_factor(n):
            factors.append(n)
            timer.track('Found a factor')

        while i * i <= n:
            if n % i == 0:
                add_factor(i)
                n //= i
            else:
                i += 1
        return factors + [n]

factors = prime_factors(600851475143)
print(factors)

thanks for catching it