From the course: Python Essential Training

Unlock the full course today

Join today to access over 23,200 courses taught by industry experts.

Solution: Finding primes faster

Solution: Finding primes faster - Python Tutorial

From the course: Python Essential Training

Solution: Finding primes faster

- [Narrator] For this challenge, as you probably figured out, it helps to keep a list of prime numbers as you go along. Not only is this list the thing that gets returned at the end of the function, but we'll use it to check for factors of the number that we're currently testing. So here's our list of primes. I'll add a two in here to start. And the number two is an odd prime. Wait, let me rephrase that. The number two is a strange prime. It's the only even prime, which makes it odd. So starting with this value, this two in our list, will help our function calculate the rest of the primes up to the number we've been given. Then, for each number between three and the number we passed into the function, we're going to get the square root. Note that I'm putting this value into a variable. I could just put a number to the 0.5 power in there, but then we'd be recalculating it every time we go through that loop. So it saves a little bit of computing power just to store the result up here…

Contents