示例#1
0
文件: fft.go 项目: kelvinlau/go
// FFT returns the fourier transform of v.
func FFT(v Poly) Poly {
	y := make(Poly, len(v))
	n := int64(len(v))
	w := number.ModularPower(omega, (modular-1)/n, modular)
	fft(v, y, modular, w, 1)
	return y
}
示例#2
0
文件: fft.go 项目: kelvinlau/go
// IFFT returns the inverted fourier transform of v.
func IFFT(v Poly) Poly {
	y := make(Poly, len(v))
	n := int64(len(v))
	w := number.ModularPower(omega, (modular-1)/n, modular)
	w1 := number.ModularInvert(w, modular)
	fft(v, y, modular, w1, 1)
	n1 := number.ModularInvert(n, modular)
	for i := range y {
		y[i] = y[i] * n1 % modular
	}
	return y
}
示例#3
0
文件: bigprime.go 项目: kelvinlau/go
// MillerRabinK test if n is a prime, testing for k times.
func MillerRabinK(n int64, k int) bool {
	if n <= 3 {
		return n > 1
	}
	if n%2 == 0 {
		return false
	}
	d := n - 1
	s := 0
	for d%2 == 0 {
		d /= 2
		s++
	}

	for ; k > 0; k-- {
		x := number.ModularPower(rand.Int63n(n-3)+2, d, n)
		if x == 1 || x == n-1 {
			continue
		}

		r := 1
		for ; r < s; r++ {
			x = number.ModularMultiply(x, x, n)
			if x == 1 {
				return false
			}
			if x == n-1 {
				break
			}
		}
		if r == s {
			return false
		}
	}
	return true
}