Quick actions

cmd+k|ctrl+k

Navigation

Languages

PythonPrimeFactors

Snippet info

Language

Python

Visibility

public

Author

linus.aspelin

Created

2017-05-05T11:41:12Z

Updated

2017-05-05T11:41:12Z

import time
start = time.time()
l = []

class factor:
    value = 0
    count = 1

def factorize(y):
    l2 = []
    while y > 1:
        for z in l:
            if y % z == 0:
                y /= z
                b = True
                for f in l2:
                    if f.value == z:
                        f.count += 1
                        b = False
                        break
                if b:
                    f2 = factor()
                    f2.value = z
                    l2.append(f2)
                break
    return l2


x = 2
#while start + 14 > time.time():
while x < 100:
    p = True
    for y in l:
        if x % y == 0:
            p = False
            break
    if p:
        l.append(x)
    print(x, end=" = ")
    f = factorize(x)
    if len(f) > 0:
        fs = []
        for fx in f:
            fx2 =  str(fx.value)
            if fx.count > 1:
                fx2 += "^" + str(fx.count)
            fs.append(fx2)
        print(" * ".join(fs))
    x += 1
    
INFO