-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathis_prime.go
67 lines (59 loc) · 1.36 KB
/
is_prime.go
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
/*
* Date: 2017-12-23
*
* Description:
* Check if a number is prime or not.
*
* Approach:
* 1. All numbers can be represented in the form of 6k + i for i = -1, 0, 1, 2,
* 3 and 4.
* 2. And 6k, 6k + 2, 6k + 4 will always be divisible by 2.
* 3. 6k + 3 will always be divisible by 3.
* 4. So we are only left with (6k - 1) and (6k + 1) which can be prime with
* exception of 2 and 3.
* 5. Also we don't have to check for 1 to n or n/2, we only have to check upto
* sqrt(n) as larger factor n must be multiple of smaller factor of n that
* has been already checked.
*
* Reference:
* http://www.geeksforgeeks.org/primality-test-set-1-introduction-and-school-method/
*
* Complexity:
* O(sqrt(n))
*/
package main
import (
"fmt"
)
func is_prime(n uint64) (bool) {
var i uint64
if (n <= 1) {
return false
}
// Special case
if (n == 2 || n == 3) {
return true
}
/*
* Check for numbers of form 6k, 6k+2, 6k+3, 6k+4, step 2 and 3 above
*/
if (n % 2 == 0 || n % 3 == 0) {
return false
}
for i = 5; i*i <= n; i = i + 6 {
if (n % i == 0 || n % (i + 2) == 0) {
return false
}
}
return true
}
func main() {
var n uint64
fmt.Println("Enter number: ")
_, err := fmt.Scanf("%d", &n)
if (err == nil) {
fmt.Println(is_prime(n))
} else {
fmt.Println("Scanf failed with error: %v", err)
}
}