Skip to content

Commit 9245e64

Browse files
author
Rafal Gajdulewicz
committed
Faster prime test
1 parent 10b52c2 commit 9245e64

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

utils/utils.go

+5-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,11 @@ import "math"
44

55
func IsPrime(num int) bool {
66
floatNum := float64(num)
7-
for i := 2; i <= int(math.Floor(math.Sqrt(floatNum))); i++ {
8-
if num%i == 0 {
7+
if (num%2 == 0) || (num%3 == 0) {
8+
return false
9+
}
10+
for i := 1; (6*i - 1) <= int(math.Floor(math.Sqrt(floatNum))); i++ {
11+
if num%(6*i-1) == 0 || num%(6*i+1) == 0 {
912
return false
1013
}
1114
}

0 commit comments

Comments
 (0)