File tree 1 file changed +39
-17
lines changed
1 file changed +39
-17
lines changed Original file line number Diff line number Diff line change 1
- import math
2
- while True :
3
- try :
4
- num = int (input ("Enter a Number: " ))
5
- break
6
- except ValueError :
7
- print ("Invalid Input" )
1
+ # Author: Tan Duc Mai
2
+
3
+ # Description: Three different functions to check whether a given number is a prime.
4
+ # Return True if it is a prime, False otherwise.
5
+ # Those three functions, from a to c, decreases in efficiency
6
+ # (takes longer time).
8
7
9
- if num > 1 :
10
- for i in range (2 ,int (math .sqrt (num ))): #Smallest Prime Factor of a Composite Number is less than or equal to Square Root of N
11
- if (num % i ) == 0 :
12
- print (num ,"is NOT a Prime Number. It's indeed a COMPOSITE NUMBER" )
13
- break
14
- else :
15
- print (num ,"is a PRIME NUMBER " )
16
-
17
- else :
18
- print (num ,"is NOT a Prime Number" )
8
+ from math import sqrt
9
+
10
+
11
+ def is_prime_a (n ):
12
+ if n < 2 :
13
+ return False
14
+ sqrt_n = int (sqrt (n ))
15
+ for i in range (2 , sqrt_n + 1 ):
16
+ if n % i == 0 :
17
+ return False
18
+ return True
19
+
20
+
21
+ def is_prime_b (n ):
22
+ if n > 1 :
23
+ if n == 2 :
24
+ return True
25
+ else :
26
+ for i in range (2 , n ):
27
+ if n % i == 0 :
28
+ return False
29
+ return True
30
+ return False
31
+
32
+
33
+ def is_prime_c (n ):
34
+ divisible = 0
35
+ for i in range (1 , n + 1 ):
36
+ if n % i == 0 :
37
+ divisible += 1
38
+ if divisible == 2 :
39
+ return True
40
+ return False
You can’t perform that action at this time.
0 commit comments