Prime Factors -suggestions?

Anyone have suggestions on how to find all prime factors of a given number in GH?

Using Python?

def prime_factors(n):
    i = 2
    factors = []
    while i * i <= n:
        if n % i:
            i += 1
        else:
            n //= i
            factors.append(i)
    if n > 1:
        factors.append(n)
    return factors

a = prime_factors(x)

From https://stackoverflow.com/questions/15347174/python-finding-prime-factors

1 Like

wow great! This is perfect. Thanks a million!
(Also this is my first time using python script component --worked like a charm!)

Great! Glad to help :slight_smile: there are doubtless more efficient algorithms out there, depending on the size of numbers you are looking at and how many times you are repeating the operation.