Primes over 300

Let P be the 30th prime number greater than 300. What is P ?


The answer is 479.

This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try refreshing the page, (b) enabling javascript if it is disabled on your browser and, finally, (c) loading the non-javascript version of this page . We're sorry about the hassle.

1 solution

The answer is 479 .

One solution is to iterate through the integers starting at 300, checking if each one is prime. Keep a counter of how many primes have been encountered. Once the counter reaches 30, you have reached the answer.

There are many ways to determine if a number n is prime. The below solution uses the sieve of Eratosthenes as its approach.

# Python solution

import math

# isPrime(n): Determines whether n is prime using the sieve of Eratosthenes
# returns True or False
def isPrime(n):
    if (n == 1):
        return False
    elif (n < 4):
        return True
    elif (n % 2 == 0):
        return False
    elif (n < 9):
        return True
    elif (n % 3 == 0):
        return False
    else:
        r = math.floor(math.sqrt(n))
        f = 5
        while (f <= r):
            if (n % f == 0):
                return False
            if (n % (f+2) == 0):
                return False
            f += 6
        return True

# loop through the numbers above 300
# check each one to see if it is prime
# when you find the 30th prime, print it out
n = 300
count = 0
while (count < 30):
    if isPrime(n):
        count += 1 
        n += 1
    else:
        n += 1
print n - 1

0 pending reports

×

Problem Loading...

Note Loading...

Set Loading...