Skip to content

Commit 073efc8

Browse files
authored
Created Readme.md for Optimised School Method
1 parent 98d85f2 commit 073efc8

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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

0 commit comments

Comments
 (0)