-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathisPrimeSearch.py
122 lines (93 loc) · 4.23 KB
/
isPrimeSearch.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
def checkForPrime1(possiblePrimeNumber):
""" Checkes if a number is a prime. Returns true or false """
if not isinstance(possiblePrimeNumber, int):
return False
if int(str(possiblePrimeNumber)[-1]) in (2, 4, 5, 6, 8, 0):
if possiblePrimeNumber == 2:
return True
elif possiblePrimeNumber == 5:
return True
return False
if possiblePrimeNumber == 1:
return False
# Vi antager at der kun skal testes for division med ulige tal, da all der kan divideres med et lige tal er frasorteret ovenfor som ikke prime
# Since the number candidate at this stage is not divvisble by five (numbers ending in five is not a prime and have been skipped earlier), we want to only test for uneven numers that are nto five - hence the different loops
if possiblePrimeNumber < 25:
for currentDivider in range(3, round(possiblePrimeNumber ** 0.5) + 1, 10):
if possiblePrimeNumber % currentDivider == 0:
return False
else:
for currentDivider in range(3, round(possiblePrimeNumber ** 0.5) + 1, 10):
if possiblePrimeNumber % currentDivider == 0:
return False
for currentDivider in range(7, round(possiblePrimeNumber ** 0.5) + 1, 10):
if possiblePrimeNumber % currentDivider == 0:
return False
for currentDivider in range(19, round(possiblePrimeNumber ** 0.5) + 1, 10):
if possiblePrimeNumber % currentDivider == 0:
return False
for currentDivider in range(11, round(possiblePrimeNumber ** 0.5) + 1, 10):
if possiblePrimeNumber % currentDivider == 0:
return False
# All numbers have been checked - no divisor with mod 0 ==> it is a prime
return True
def checkForPrime2(possiblePrimeNumber):
""" Checkes if a number is a prime. Returns true or false """
if int(str(possiblePrimeNumber)[-1]) in (2, 4, 5, 6, 8, 0):
if possiblePrimeNumber == 2:
return True
elif possiblePrimeNumber == 5:
return True
return False
for currentDivider in range(2, round(possiblePrimeNumber ** 0.5) + 1):
if possiblePrimeNumber % currentDivider == 0:
return False
return True
def checkForPrime3(possiblePrimeNumber):
""" Checkes if a number is a prime. Returns true or false """
for currentDivider in range(2, round(possiblePrimeNumber ** 0.5) + 1):
if possiblePrimeNumber % currentDivider == 0:
return False
return True
def checkForPrime4(possiblePrimeNumber):
""" Checkes if a number is a prime. Returns true or false """
for currentDivider in range(2, round(possiblePrimeNumber * 0.5) + 1):
if possiblePrimeNumber % currentDivider == 0:
return False
return True
def checkForPrime5(possiblePrimeNumber):
if not isinstance(possiblePrimeNumber, int):
return False
# Corner cases
if (possiblePrimeNumber <= 1):
return False
if (possiblePrimeNumber <= 3):
return True
if (possiblePrimeNumber % 2 == 0 or possiblePrimeNumber % 3 == 0):
return False
i = 5
while (i * i <= possiblePrimeNumber):
if (possiblePrimeNumber % i == 0 or possiblePrimeNumber % (i + 2) == 0):
return False
i = i + 6
return True
def checkForPrime6(possiblePrimeNumber):
if not isinstance(possiblePrimeNumber, int):
return False
# Corner cases
if (possiblePrimeNumber <= 1):
return False
if (possiblePrimeNumber <= 3):
return True
if (possiblePrimeNumber % 2 == 0 or possiblePrimeNumber % 3 == 0):
return False
i = 5
while (i * i <= possiblePrimeNumber):
if (possiblePrimeNumber % i == 0 or possiblePrimeNumber % (i + 6) == 0 or possiblePrimeNumber % (
i + 12) == 0 or possiblePrimeNumber % (i + 18) == 0 or possiblePrimeNumber % (i + 24) == 0
or possiblePrimeNumber % (i + 2) == 0 or possiblePrimeNumber % (
i + 2 + 6) == 0 or possiblePrimeNumber % (i + 2 + 12) == 0 or possiblePrimeNumber % (
i + 2 + 24) == 0 or possiblePrimeNumber % (i + 2 + 30) == 0):
return False
i = i + 36
return True