-
Notifications
You must be signed in to change notification settings - Fork 181
/
primeNumbers.py
50 lines (41 loc) · 1.5 KB
/
primeNumbers.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# Prime number Determiner
# replace input() with raw_input() in Python version 2.7 input() works with version 3
import math as Math
POSITIVE_MESSAGE = " is a prime number"
NEGATIVE_MESSAGE = " is not a prime number"
def is_number_prime(number):
"""
Function which checks whether the number is a prime number or not
:param number: integer - to be checked for prime-ness
:return: boolean - true if prime, else false
"""
"""
This is the main logic behind reducing the numbers to check for as factors
if N = a * b; where a<=b and a,b C (1, N)
then, a * b >= a*a;
which leads to => a*a <= N
=> a <= sqrt(N)
Hence checking only till the square root of N
"""
upper_lim = Math.floor(Math.sqrt(number)) + 1
is_prime = True if number != 1 else False
for i in range(2, upper_lim):
if number % i == 0:
is_prime = False
break
# The moment there is a divisor of 'number', break the iteration, as the number is not prime
return is_prime
while True:
startOrEnd = str(input('Start or End : '))
if startOrEnd == 'Start':
number = int(input('Number to Check : '))
result = str(number)
prime_status = is_number_prime(number)
if prime_status:
result += POSITIVE_MESSAGE
else:
result += NEGATIVE_MESSAGE
print(result)
else:
print('Program Ended...')
break