// Copyright (c) 2014 The mathutil Authors. All rights reserved.// Use of this source code is governed by a BSD-style// license that can be found in the LICENSE file.package mathutil // import "modernc.org/mathutil"import ()// IsPrimeUint16 returns true if n is prime. Typical run time is few ns.func ( uint16) bool {return > 0 && primes16[-1] == 1}// NextPrimeUint16 returns first prime > n and true if successful or an// undefined value and false if there is no next prime in the uint16 limits.// Typical run time is few ns.func ( uint16) ( uint16, bool) {return + uint16(primes16[]), < 65521}// IsPrime returns true if n is prime. Typical run time is about 100 ns.func ( uint32) bool {switch {case &1 == 0:return == 2case %3 == 0:return == 3case %5 == 0:return == 5case %7 == 0:return == 7case %11 == 0:return == 11case %13 == 0:return == 13case %17 == 0:return == 17case %19 == 0:return == 19case %23 == 0:return == 23case %29 == 0:return == 29case %31 == 0:return == 31case %37 == 0:return == 37case %41 == 0:return == 41case %43 == 0:return == 43case %47 == 0:return == 47case %53 == 0:return == 53// Benchmarked optimumcase < 65536:// use table datareturnIsPrimeUint16(uint16())default: := ModPowUint32(2, (+1)/2, )if != 2 && != -2 {returnfalse } := &lohi[>>24] , := .lo, .hifor <= { := ( + ) >> 1 := liars[]switch {case > : = + 1case < : = - 1default:returnfalse } }returntrue }}// IsPrimeUint64 returns true if n is prime. Typical run time is few tens of µs.//// SPRP bases: http://miller-rabin.appspot.comfunc ( uint64) bool {switch {case %2 == 0:return == 2case %3 == 0:return == 3case %5 == 0:return == 5case %7 == 0:return == 7case %11 == 0:return == 11case %13 == 0:return == 13case %17 == 0:return == 17case %19 == 0:return == 19case %23 == 0:return == 23case %29 == 0:return == 29case %31 == 0:return == 31case %37 == 0:return == 37case %41 == 0:return == 41case %43 == 0:return == 43case %47 == 0:return == 47case %53 == 0:return == 53case %59 == 0:return == 59case %61 == 0:return == 61case %67 == 0:return == 67case %71 == 0:return == 71case %73 == 0:return == 73case %79 == 0:return == 79case %83 == 0:return == 83case %89 == 0:return == 89// Benchmarked optimumcase <= math.MaxUint16:returnIsPrimeUint16(uint16())case <= math.MaxUint32:returnProbablyPrimeUint32(uint32(), 11000544) &&ProbablyPrimeUint32(uint32(), 31481107)case < 105936894253:returnProbablyPrimeUint64_32(, 2) &&ProbablyPrimeUint64_32(, 1005905886) &&ProbablyPrimeUint64_32(, 1340600841)case < 31858317218647:returnProbablyPrimeUint64_32(, 2) &&ProbablyPrimeUint64_32(, 642735) &&ProbablyPrimeUint64_32(, 553174392) &&ProbablyPrimeUint64_32(, 3046413974)case < 3071837692357849:returnProbablyPrimeUint64_32(, 2) &&ProbablyPrimeUint64_32(, 75088) &&ProbablyPrimeUint64_32(, 642735) &&ProbablyPrimeUint64_32(, 203659041) &&ProbablyPrimeUint64_32(, 3613982119)default:returnProbablyPrimeUint64_32(, 2) &&ProbablyPrimeUint64_32(, 325) &&ProbablyPrimeUint64_32(, 9375) &&ProbablyPrimeUint64_32(, 28178) &&ProbablyPrimeUint64_32(, 450775) &&ProbablyPrimeUint64_32(, 9780504) &&ProbablyPrimeUint64_32(, 1795265022) }}// NextPrime returns first prime > n and true if successful or an undefined value and false if there// is no next prime in the uint32 limits. Typical run time is about 2 µs.func ( uint32) ( uint32, bool) {switch {case < 65521: , := NextPrimeUint16(uint16())returnuint32(), truecase >= math.MaxUint32-4:return } ++var , uint32switch := % 6; {case0: , = 1, 4case1: = 4case2, 3, 4: , = 5-, 2case5: = 2 } = + if < { // overflowreturn }for {ifIsPrime() {return , true } := += if < { // overflowbreak } ^= 6 }return}// NextPrimeUint64 returns first prime > n and true if successful or an undefined value and false if there// is no next prime in the uint64 limits. Typical run time is in hundreds of µs.func ( uint64) ( uint64, bool) {switch {case < 65521: , := NextPrimeUint16(uint16())returnuint64(), truecase >= 18446744073709551557: // last uint64 primereturn } ++var , uint64switch := % 6; {case0: , = 1, 4case1: = 4case2, 3, 4: , = 5-, 2case5: = 2 } = + if < { // overflowreturn }for {if = IsPrimeUint64(); {break } := += if < { // overflowbreak } ^= 6 }return}// FactorTerm is one term of an integer factorization.typeFactorTermstruct { Prime uint32// The divisor Power uint32// Term == Prime^Power}// FactorTerms represent a factorization of an integertypeFactorTerms []FactorTerm// FactorInt returns prime factorization of n > 1 or nil otherwise.// Resulting factors are ordered by Prime. Typical run time is few µs.func ( uint32) ( FactorTerms) {switch {case < 2:returncaseIsPrime():return []FactorTerm{{, 1}} } , := make([]FactorTerm, 9), 0for := 2; < len(primes16); += int(primes16[]) {ifuint(*) > uint() {break } := uint32(0)for %uint32() == 0 { /= uint32() ++ }if != 0 { [] = FactorTerm{uint32(), } ++ }if == 1 {break } }if != 1 { [] = FactorTerm{, 1} ++ }return [:]}// PrimorialProductsUint32 returns a slice of numbers in [lo, hi] which are a// product of max 'max' primorials. The slice is not sorted.//// See also: http://en.wikipedia.org/wiki/Primorialfunc (, , uint32) ( []uint32) { , := int64(), int64()if > 31 { // N/A = 31 }varfunc(int64, int64, uint32) = func(, int64, uint32) { := uint32(1)for <= && <= { *= if >= && <= { = append(, uint32()) }if < { , := NextPrime(uint32()) (, int64(), ) } ++ } } (1, 2, )return}
The pages are generated with Goldsv0.3.6. (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.