Skip to content

Commit 04a6227

Browse files
committed
gmp.jl: fix typo and add comment for prod on BigInt
1 parent 1b7a095 commit 04a6227

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

base/gmp.jl

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -477,7 +477,7 @@ end
477477

478478
function powermod(x::BigInt, p::BigInt, m::BigInt)
479479
r = MPZ.powm(x, p, m)
480-
return m < 0 && r > 0 ? MPZ.add!(r, m) : r # choose sign conistent with mod(x^p, m)
480+
return m < 0 && r > 0 ? MPZ.add!(r, m) : r # choose sign consistent with mod(x^p, m)
481481
end
482482

483483
powermod(x::Integer, p::Integer, m::BigInt) = powermod(big(x), big(p), m)
@@ -501,6 +501,12 @@ function gcdx(a::BigInt, b::BigInt)
501501
end
502502

503503
sum(arr::AbstractArray{BigInt}) = foldl(MPZ.add!, BigInt(0), arr)
504+
# note: a similar implementation for `prod` won't be efficient:
505+
# 1) the time complexity of the allocations is negligible compared to the multiplications
506+
# 2) assuming arr contains similarly sized BigInts, the multiplications are much more
507+
# performant when doing e.g. ((a1*a2)*(a2*a3))*(...) rather than a1*(a2*(a3*(...))),
508+
# which is exactly what the default implementation of `prod` does, via mapreduce
509+
# (which maybe could be slightly optimized for BigInt)
504510

505511
factorial(x::BigInt) = isneg(x) ? BigInt(0) : MPZ.fac_ui(x)
506512

0 commit comments

Comments
 (0)