- 注册时间
- 2008-2-6
- 最后登录
- 1970-1-1
- 威望
- 星
- 金币
- 枚
- 贡献
- 分
- 经验
- 点
- 鲜花
- 朵
- 魅力
- 点
- 上传
- 次
- 下载
- 次
- 积分
- 51573
- 在线时间
- 小时
|
楼主 |
发表于 2020-11-28 10:53:49
|
显示全部楼层
- using Primes
- struct Quadratic{T <: Integer, C} <: Integer
- re::T
- ir::T
- end
- function qfmul(left:: Quadratic{T, C}, right:: Quadratic{T, C}) where {T <: Integer, C}
- re = left.re * right.re + left.ir * right.ir * C
- ir = left.re * right.ir + left.ir * right.re
- return Quadratic{T, C}(re, ir)
- end
- function qfmod(left:: Quadratic{T, C}, r:: T) where {T <: Integer, C}
- re = left.re % r
- ir = left.ir % r
- return Quadratic{T, C}(re, ir)
- end
- function qfmulmod(left:: Quadratic{T, C}, right:: Quadratic{T, C}, r::T) where {T <: Integer, C}
- re = (left.re * right.re + left.ir * right.ir * C) % r
- ir = (left.re * right.ir + left.ir * right.re) % r
- return Quadratic{T, C}(re, ir)
- end
- function qfpowermod(x::Quadratic{T, C}, p::T , m::T) where {T <: Integer, C}
- @assert p >= 0
- p == 0 && return Quadratic{T, C}(1, 0)
- #b = oftype(m,mod(x,m)) # this also checks for divide by zero
- b = x
- t = prevpow(2, p)
- r = Quadratic{T, C}(1, 0)
- while true
- if p >= t
- r = qfmulmod(r, b, m)
- p -= t
- end
- t >>>= 1
- t <= 0 && break
- r = qfmulmod(r, r ,m)
- end
- return r
- end
- function jacobi(a, b)
- tab2 = [0, 1, 0, -1, 0, -1, 0, 1]
- if b == 0
- if abs(a)==1
- return 1
- else
- return 0
- end
- end
- if iseven(a) && iseven(b)
- return 0
- end
- v = trailing_zeros(b)
- b = b >> v
- if iseven(v)
- k = 1
- else
- k = tab2[a & 7 + 1]
- end
- if b < 0
- b = -b
- end
- if a < 0
- k = -k
- end
- while a != 0
- v = trailing_zeros(a)
- a = a >> v
- if isodd(v)
- k *= tab2[b & 7 + 1]
- end
- if a & b & 3 == 3
- k = -k
- end
- r = abs(a)
- a = b % r
- b = r
- end
- if b > 1
- return 0
- else
- return k
- end
- end
- function frobenius(n)
- small = [
- 3, 5, 7, 11, 13, 17, 19, 23,
- 29, 31, 37, 41, 43, 47, 53, 59,
- 61, 67, 71, 73, 79, 83, 89, 97,
- 101,103,107,109,113,127,131,137
- ]
- find = false
- global p = 0
- for c in small
- if jacobi(c, n) == -1
- find = true
- global p = c
- break
- end
- end
- if !find
- c = Primes.nextprime(last(small)+1)
- while jacobi(c, n) != -1
- c = Primes.nextprime(c+1)
- end
- global p = c
- end
- #println("$n: $p")
- b = Quadratic{typeof(n), p}(1, 1)
- r = qfpowermod(b, n, n)
- return (r.re == 1) && (r.ir == n - 1)
- end
- function fermatProbablePrimeTest(n, b)
- if gcd(n, b) > 1
- return false
- end
- a = powermod(b, (n-1)>>1, n)
- return (a == 1) || (a == (n-1))
- end
- function millerRabinProbablePrimeTest( n, b )
- k = trailing_zeros(n-1)
- m = (n-1) >> k
- a = powermod(b, m, n)
- if (a == 1) || (a == (n-1))
- return true
- else
- for i in 1:k-1
- a = powermod(a, 2, n)
- if a == n - 1
- return true
- end
- end
- end
- return false
- end
- global t = 0
- global b = big(10)^28
- println("==========")
- @time for i in range(1, step = 2, stop = 9999999)
- test = b + i
- # if isqrt(test)^2 == test
- # continue
- # end
- if fermatProbablePrimeTest(test, 2)
- if millerRabinProbablePrimeTest(test, 2)
- #if frobenius(test)
- if !isprime(test)
- #println("$test is frobenius pseudoprime!")
- #else
- #println("$test is prime")
- #end
- #else
- println("$test is SPSP(2)")
- end
- else
- println("$test is PSP(2)")
- end
- end
- end
- println("==========")
复制代码 |
|