Write code.
— 1 min read

Finding prime factors of x

In a previous post, we showed how any number can be written, in a unique way, as the product of prime numbers.

In this post we will make a simple algorithm to compute this prime numbers.

 1 def get_primes(x):
 2     non_primes = set()
 3     primes = set()
 4     for i in range(2, x+1):
 5         if x%i == 0 and i not in non_primes:
 6             primes.add(i)
 7             j = i
 8             while j*i <= x:
 9                 non_primes.add(i*j)
10                 j += 1
11     return primes