Source File
prime.go
Belonging Package
math/big
package big
import
const uint64 = 1<<2 | 1<<3 | 1<<5 | 1<<7 |
1<<11 | 1<<13 | 1<<17 | 1<<19 | 1<<23 | 1<<29 | 1<<31 |
1<<37 | 1<<41 | 1<<43 | 1<<47 | 1<<53 | 1<<59 | 1<<61
:= .abs[0]
if len(.abs) == 1 && < 64 {
return &(1<<) != 0
}
if &1 == 0 {
return false // x is even
}
const = 3 * 5 * 7 * 11 * 13 * 17 * 19 * 23 * 37
const = 29 * 31 * 41 * 43 * 47 * 53
var , uint32
switch _W {
case 32:
= uint32(.abs.modW())
= uint32(.abs.modW())
case 64:
:= .abs.modW(( * ) & _M)
= uint32( % )
= uint32( % )
default:
panic("math/big: invalid word size")
}
if %3 == 0 || %5 == 0 || %7 == 0 || %11 == 0 || %13 == 0 || %17 == 0 || %19 == 0 || %23 == 0 || %37 == 0 ||
%29 == 0 || %31 == 0 || %41 == 0 || %43 == 0 || %47 == 0 || %53 == 0 {
return false
}
return .abs.probablyPrimeMillerRabin(+1, true) && .abs.probablyPrimeLucas()
}
:= .trailingZeroBits()
:= nat(nil).shr(, )
:= nat(nil).sub(, natTwo)
:= rand.New(rand.NewSource(int64([0])))
var , , nat
:= .bitLen()
:
for := 0; < ; ++ {
if == -1 && {
= .set(natTwo)
} else {
= .random(, , )
= .add(, natTwo)
}
= .expNN(, , )
if .cmp(natOne) == 0 || .cmp() == 0 {
continue
}
for := uint(1); < ; ++ {
= .sqr()
, = .div(, , )
if .cmp() == 0 {
continue
}
if .cmp(natOne) == 0 {
return false
}
}
return false
}
return true
}
return len() == 1 && [0] == +2
}
![]() |
The pages are generated with Golds v0.3.2-preview. (GOOS=darwin GOARCH=amd64) Golds is a Go 101 project developed by Tapir Liu. PR and bug reports are welcome and can be submitted to the issue list. Please follow @Go100and1 (reachable from the left QR code) to get the latest news of Golds. |