$\mathfrak{Mini}$ $\mathbb{Wiki}$
Please remove syntax.php from lib/plugins/nodisp or remove the plugin and reinstall

4. April 2021 mozgan   

This is an old revision of the document!


Find Nth Prime

This algorithm finds the $N$th prime number:

#!/usr/bin/env python3
 
import math
 
def is_prime(n):
    if (n < 2) or (n % 2 == 0):
        return False
    elif n == 2 or n == 3:
        return True
 
    k = int(math.sqrt(n))
    for i in range(3, k+1, 2):
        if n % i == 0:
            return False
    return True
 
 
if __name__ == '__main__':
    n = int(input())
    cnt = 1
 
    if n == 1:
        print(2)
    else:
        i = 1
        while (True):
            i += 2
            if is_prime(i) == True:
                cnt += 1
                #print(i) # -> gives full list upto nth prime
                if cnt == n:
                    print(i)
                    break
This website uses cookies. By using the website, you agree with storing cookies on your computer. Also you acknowledge that you have read and understand our Privacy Policy. If you do not agree leave the website.More information about cookies