Ann

In my last blog, I showed you the slower version of how to tell if a number is prime or not. Last time, I promised you that I’d show you the faster version in my next blog.

So, here it is:

x = 17
i = 0
length = len(the_primes)
the_primes = [2,3,5,7,11,13]
is_prime = True
while i < length:
if x % the_primes[i] == 0:
is_prime = False
break
i = i + 1
if is_prime == True:
print((f'{x} is a prime number!'))
else:
print((f'{x} is not a prime number'))

Input: x = 17

Output: 17 is a prime number!

I found out that you really don’t have to divide it by all the numbers before the number. You only have to divide it by the prime numbers before it.

--

--

A prime number is a number greater than 1 that is not a product of two smaller natural numbers. For example, 3 is prime because the only ways of writing it as a product, 1 × 3 or 3 × 1, involve 3 itself. But 6 isn’t a prime number…

--

--

Ann

Ann

Hi! I'm an elementary school student.