Skip to content

Commit bf2b6c5

Browse files
authored
Merge pull request deutranium#87 from Hamzakam/optimised-school-method
Created Readme.md for Optimised School Method in Primality tests.
2 parents 915bd75 + af776b5 commit bf2b6c5

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Optimised School Method
2+
3+
- Checks whether a Given Positive Integer is Prime or not.
4+
- Time Complexity:
5+
- Worst Case Complexity:O(√n)
6+
- Best Case Complexity:O(1)
7+
- Average Case Complexity:O(√n)
8+
- Worst Case Space Complexity:O(1)
9+
10+
### Logic
11+
12+
1. Firstly We Only check till √n numbers from 3.
13+
2. We further Optimised the Algorithm by observing the pattern that all Prime Numbers are of the Form 6k ± 1 with exception of 1 and 2.
14+
3. We check whether all (i+6)<sup>th</sup> number are divisible by given number and iterate till the &Sqrt;n<sup>th</sup> number.
15+
4. Pseudo Code:
16+
17+
18+
function is_prime(n)
19+
if n ≤ 3 then
20+
return n > 1
21+
else if n mod 2 = 0 or n mod 3 = 0
22+
return false
23+
24+
let i ← 5
25+
while i × i ≤ n do
26+
if n mod i = 0 or n mod (i + 2) = 0
27+
return false
28+
i ← i + 6
29+
30+
return true
31+
### Instruction for Running code:
32+
33+
- Python
34+
```
35+
python3 optimisedSchoolMethod.py
36+
```

0 commit comments

Comments
 (0)