|
1 |
| -# This includes parts that were formerly a part of Julia. License is MIT: http://julialang.org/license |
2 |
| -VERSION < v"0.7.0-beta2.199" && __precompile__() |
| 1 | +# This file includes code that was formerly a part of Julia. License is MIT: http://julialang.org/license |
| 2 | + |
3 | 3 | module Primes
|
4 | 4 |
|
5 | 5 | using Base.Iterators: repeated
|
@@ -338,13 +338,8 @@ Set([2,5])
|
338 | 338 | """
|
339 | 339 | factor(::Type{D}, n::T) where {T<:Integer, D<:AbstractDict} = factor!(n, D(Dict{T,Int}()))
|
340 | 340 | factor(::Type{A}, n::T) where {T<:Integer, A<:AbstractArray} = A(factor(Vector{T}, n))
|
341 |
| -if VERSION >= v"0.7.0-beta.81" |
342 |
| - factor(::Type{Vector{T}}, n::T) where {T<:Integer} = |
343 |
| - mapreduce(collect, vcat, [repeated(k, v) for (k, v) in factor(n)], init=Vector{T}()) |
344 |
| -else |
345 |
| - factor(::Type{Vector{T}}, n::T) where {T<:Integer} = |
346 |
| - mapreduce(collect, vcat, Vector{T}(), [repeated(k, v) for (k, v) in factor(n)]) |
347 |
| -end |
| 341 | +factor(::Type{Vector{T}}, n::T) where {T<:Integer} = |
| 342 | + mapreduce(collect, vcat, [repeated(k, v) for (k, v) in factor(n)], init=Vector{T}()) |
348 | 343 | factor(::Type{S}, n::T) where {T<:Integer, S<:Union{Set,BitSet}} = S(keys(factor(n)))
|
349 | 344 | factor(::Type{T}, n) where {T<:Any} = throw(MethodError(factor, (T, n)))
|
350 | 345 |
|
@@ -435,12 +430,6 @@ function pollardfactors!(n::T, h::AbstractDict{K,Int}) where {T<:Integer,K<:Inte
|
435 | 430 | end
|
436 | 431 | end
|
437 | 432 |
|
438 |
| -if VERSION >= v"0.7.0-beta.183" |
439 |
| - ndigits2(n) = ndigits(n, base=2) |
440 |
| -else |
441 |
| - ndigits2(n) = ndigits(n, 2) |
442 |
| -end |
443 |
| - |
444 | 433 | """
|
445 | 434 | ismersenneprime(M::Integer; [check::Bool = true]) -> Bool
|
446 | 435 |
|
|
461 | 450 | """
|
462 | 451 | function ismersenneprime(M::Integer; check::Bool = true)
|
463 | 452 | if check
|
464 |
| - d = ndigits2(M) |
| 453 | + d = ndigits(M, base=2) |
465 | 454 | M >= 0 && isprime(d) && (M >> d == 0) ||
|
466 | 455 | throw(ArgumentError("The argument given is not a valid Mersenne Number (`M = 2^p - 1`)."))
|
467 | 456 | end
|
|
486 | 475 | ```
|
487 | 476 | """
|
488 | 477 | function isrieselprime(k::Integer, Q::Integer)
|
489 |
| - n = ndigits2(Q) |
| 478 | + n = ndigits(Q, base=2) |
490 | 479 | 0 < k < Q || throw(ArgumentError("The condition 0 < k < Q must be met."))
|
491 | 480 | if k == 1 && isodd(n)
|
492 | 481 | return n % 4 == 3 ? ll_primecheck(Q, 3) : ll_primecheck(Q)
|
|
500 | 489 |
|
501 | 490 | # LL backend -- not for export
|
502 | 491 | function ll_primecheck(X::Integer, s::Integer = 4)
|
503 |
| - S, N = BigInt(s), BigInt(ndigits2(X)) |
| 492 | + S, N = BigInt(s), BigInt(ndigits(X, base=2)) |
504 | 493 | X < 7 && throw(ArgumentError("The condition X ≥ 7 must be met."))
|
505 | 494 | for i in 1:(N - 2)
|
506 | 495 | S = (S^2 - 2) % X
|
@@ -543,12 +532,10 @@ end
|
543 | 532 |
|
544 | 533 | # modify a BigInt in-place
|
545 | 534 | function add_!(n::BigInt, x::Int)
|
546 |
| - # TODO: Change `Any` to `Ref{BigInt}` when 0.6 support is dropped. |
547 |
| - # The two have the same effect but `Ref{BigInt}` won't be optimized on 0.6. |
548 | 535 | if x < 0
|
549 |
| - ccall((:__gmpz_sub_ui, :libgmp), Cvoid, (Any, Any, Culong), n, n, -x) |
| 536 | + ccall((:__gmpz_sub_ui, :libgmp), Cvoid, (Ref{BigInt}, Ref{BigInt}, Culong), n, n, -x) |
550 | 537 | else
|
551 |
| - ccall((:__gmpz_add_ui, :libgmp), Cvoid, (Any, Any, Culong), n, n, x) |
| 538 | + ccall((:__gmpz_add_ui, :libgmp), Cvoid, (Ref{BigInt}, Ref{BigInt}, Culong), n, n, x) |
552 | 539 | end
|
553 | 540 | n
|
554 | 541 | end
|
|
0 commit comments