File tree Expand file tree Collapse file tree 2 files changed +38
-0
lines changed
Python/n-digit-largestprime Expand file tree Collapse file tree 2 files changed +38
-0
lines changed Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
You can’t perform that action at this time.
0 commit comments