Thursday, July 24, 2014
Finally, it gets interesting. Below is my third attempt on this problem, the first taking so long to complete that I improved it before its first iteration, and the second just wasn’t good enough.
def find_primes(number): """Returns a set of primes""" primes = set() if number != int(number): raise Exception('Must be int') # Improve this if number < 4: return primes i = 2 while number > 1: while number % i == 0: number = number / i primes.add(i) i += 1 return primes
In this solution, I learned some basic uses for python’s set builtin. I’m a better person today.
Comments are closed, but you can leave a trackback.