7: 10001st Prime#
By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13.
What is the 10 001st prime number?
Your Solution#
Lunch this notebook and try to create your own solution!
Tip: look for the launch button (π) in the top right corner!
def problem7(N): # N = Nth prime number
# Try your solution here
return solution
# Testing:
unittest.main(argv=[''], verbosity=2,exit=False)
# Printing Project Euler Solution
# print(problem7(10_001))
My solution#
Spoiler Alert!!
See my solution bellow
My solution has two parts:
Define an βis prime?β function to verify if a number is prime or not
Define a loop function to run over a series of number until find the desired prime
def is_prime(n):
# Corner cases
if (n < 2):
return False
# Base cases
for i in range(2, n//2+1, 1):
if (n % i == 0):
return False
return True
def problem7(N): # N = Nth prime number
n = 0
i = 0
while n<N:
i += 1
if is_prime(i):
n += 1
return i
# Testing
unittest.main(argv=[''], verbosity=2,exit=False)
The 10 001st prime number is:
# Printing Project Euler Solution
print(problem7(10_001))
104743