File tree Expand file tree Collapse file tree 1 file changed +36
-0
lines changed
primalityTest/optimisedSchoolMethod Expand file tree Collapse file tree 1 file changed +36
-0
lines changed Original file line number Diff line number Diff line change
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 &Sqrt ; 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
+ ```
You can’t perform that action at this time.
0 commit comments