-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprimes.go
60 lines (50 loc) · 977 Bytes
/
primes.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
// Copyright (c) 2021 Silvano DAL ZILIO
//
// MIT License
package rudd
import "math/big"
// functions for Prime number calculations
func hasFactor(src int, n int) bool {
if (src != n) && (src%n == 0) {
return true
}
return false
}
func hasEasyFactors(src int) bool {
return hasFactor(src, 3) || hasFactor(src, 5) || hasFactor(src, 7) || hasFactor(src, 11) || hasFactor(src, 13)
}
func primeGte(src int) int {
if src%2 == 0 {
src++
}
for {
if hasEasyFactors(src) {
src = src + 2
continue
}
// ProbablyPrime is 100% accurate for inputs less than 2⁶⁴.
if big.NewInt(int64(src)).ProbablyPrime(0) {
return src
}
src = src + 2
}
}
func primeLte(src int) int {
if src == 0 {
return 1
}
if src%2 == 0 {
src--
}
for {
if hasEasyFactors(src) {
src = src - 2
continue
}
// ProbablyPrime is 100% accurate for inputs less than 2⁶⁴.
if big.NewInt(int64(src)).ProbablyPrime(0) {
return src
}
src = src - 2
}
}