Skip to content

Commit 2817084

Browse files
Merge pull request #11477 from aiorla/master
Fixing types conflict in pollardfactors!
2 parents 7f55b75 + cf5cbcc commit 2817084

File tree

2 files changed

+14
-11
lines changed

2 files changed

+14
-11
lines changed

base/primes.jl

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -99,30 +99,30 @@ function factor{T<:Integer}(n::T)
9999
isprime(n) && (h[n] = 1; return h)
100100
end
101101
end
102-
pollardfactors!(n, h)
102+
T <: BigInt || widemul(n-1,n-1) <= typemax(n) ? pollardfactors!(n, h) : pollardfactors!(widen(n), h)
103103
end
104104

105-
function pollardfactors!{T<:Integer}(n::T, h::Dict{T,Int})
105+
function pollardfactors!{T<:Integer,K<:Integer}(n::T, h::Dict{K,Int})
106106
while true
107-
local c::T = rand(1:(n-1)), G::T = 1, r::T = 1, y::T = rand(0:(n-1)), m::T = 1900
107+
local c::T = rand(1:(n-1)), G::T = 1, r::K = 1, y::T = rand(0:(n-1)), m::K = 1900
108108
local ys::T, q::T = 1, x::T
109109
while c == n - 2
110110
c = rand(1:(n-1))
111111
end
112112
while G == 1
113113
x = y
114114
for i in 1:r
115-
y = widemul(y,y)%n
116-
y = (widen(y)+widen(c))%n
115+
y = (y*y)%n
116+
y = (y+c)%n
117117
end
118-
local k::T = 0
118+
local k::K = 0
119119
G = 1
120120
while k < r && G == 1
121121
for i in 1:(m>(r-k)?(r-k):m)
122122
ys = y
123-
y = widemul(y,y)%n
124-
y = (widen(y)+widen(c))%n
125-
q = widemul(q,x>y?x-y:y-x)%n
123+
y = (y*y)%n
124+
y = (y+c)%n
125+
q = (q*(x>y?x-y:y-x))%n
126126
end
127127
G = gcd(q,n)
128128
k = k + m
@@ -131,8 +131,8 @@ function pollardfactors!{T<:Integer}(n::T, h::Dict{T,Int})
131131
end
132132
G == n && (G = 1)
133133
while G == 1
134-
ys = widemul(ys,ys)%n
135-
ys = (widen(ys)+widen(c))%n
134+
ys = (ys*ys)%n
135+
ys = (ys+c)%n
136136
G = gcd(x>ys?x-ys:ys-x,n)
137137
end
138138
if G != n

test/numbers.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2129,6 +2129,9 @@ end
21292129
@test factor((big(2)^31-1)^2) == Dict(big(2^31-1) => 2)
21302130
@test factor((big(2)^31-1)*(big(2)^17-1)) == Dict(big(2^31-1) => 1, big(2^17-1) => 1)
21312131

2132+
# fast factorization of Int128 (#11477)
2133+
@test factor((Int128(2)^39-7)^2) == Dict(Int128(2)^39-7 => 2)
2134+
21322135
# large shift amounts
21332136
@test Int32(-1)>>31 == -1
21342137
@test Int32(-1)>>32 == -1

0 commit comments

Comments
 (0)