Skip to content

Commit 5f10cf7

Browse files
authored
Merge pull request #234 from japneetbhatia/main
Program to find N digit Largest Prime Number in Python
2 parents 3a04433 + 2932fef commit 5f10cf7

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

Python/n-digit-largestprime/README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
TO FIND N-DIGIT LARGEST PRIME NUMBER
2+
3+
4+
SAMPLE- 1
5+
6+
INPUT : 2
7+
OUTPUT : 97
8+
9+
SAMPLE- 2
10+
11+
INPUT : 8
12+
OUTPUT : 99999989
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# To find N digit Largest Prime Number
2+
3+
from math import sqrt
4+
MAX = 100000000
5+
prime = [True] * (MAX + 1)
6+
7+
def SieveOfEratosthenes() :
8+
for p in range(2, int(sqrt(MAX)) + 1) :
9+
if (prime[p] == True) :
10+
for i in range(p * p, MAX + 1, p) :
11+
prime[i] = False
12+
13+
#function to find largest prime number
14+
def largestPrime(d) :
15+
l = 10 ** (d - 1)
16+
r = (10 ** d) - 1
17+
for i in range(r, l , -1) :
18+
if (prime[i]) :
19+
return i
20+
return -1
21+
22+
#driver code
23+
if __name__ == "__main__" :
24+
N = int(input("Enter N digit")) #input
25+
SieveOfEratosthenes() #call to SieveOfEratosthenes() function
26+
print(largestPrime(N)) #call to function and output

0 commit comments

Comments
 (0)