Skip to content

Commit

Permalink
Deprecate prime number functions (isprime, primes, primesmask, …
Browse files Browse the repository at this point in the history
…`factor`)

See #16357
  • Loading branch information
simonbyrne committed May 20, 2016
1 parent bf1debc commit ab31a15
Show file tree
Hide file tree
Showing 8 changed files with 27 additions and 258 deletions.
26 changes: 23 additions & 3 deletions base/primes.jl → base/deprecated-primes.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# This file is a part of Julia. License is MIT: http://julialang.org/license

export isprime, primes, primesmask, factor


function isprime(x::BigInt, reps=25)
depwarn("isprime(...) has been deprecated, use isprime(...) in Primes.jl instead", :isprime)
ccall((:__gmpz_probab_prime_p,:libgmp), Cint, (Ptr{BigInt}, Cint), &x, reps) > 0
end

# Primes generating functions
# https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes
# https://en.wikipedia.org/wiki/Wheel_factorization
Expand Down Expand Up @@ -55,6 +63,8 @@ end

# Sieve of the primes from lo up to hi represented as an array of booleans
function primesmask(lo::Int, hi::Int)
depwarn("primesmask(...) has been deprecated, use primesmask(...) in Primes.jl instead", :primesmask)

0 < lo hi || throw(ArgumentError("the condition 0 < lo ≤ hi must be met"))
sieve = falses(hi - lo + 1)
lo 2 hi && (sieve[3-lo] = true)
Expand All @@ -77,6 +87,8 @@ primesmask(n::Integer) = n <= typemax(Int) ? primesmask(Int(n)) :
throw(ArgumentError("requested number of primes must be ≤ $(typemax(Int)), got $n"))

function primes(lo::Int, hi::Int)
depwarn("primes(...) has been deprecated, use primes(...) in Primes.jl instead", :primes)

lo hi || throw(ArgumentError("the condition lo ≤ hi must be met"))
list = Int[]
lo 2 hi && push!(list, 2)
Expand All @@ -99,6 +111,8 @@ const PRIMES = primes(2^16)
# https://en.wikipedia.org/wiki/Miller–Rabin_primality_test
#
function isprime(n::Integer)
depwarn("isprime(...) has been deprecated, use isprime(...) in Primes.jl instead", :isprime)

(n < 3 || iseven(n)) && return n == 2
n <= 2^16 && return PRIMES[searchsortedlast(PRIMES,n)] == n
s = trailing_zeros(n-1)
Expand Down Expand Up @@ -130,10 +144,14 @@ witnesses(n::Union{UInt64,Int64}) =
n < 3474749660383 ? (2,3,5,7,11,13) :
(2,325,9375,28178,450775,9780504,1795265022)

isprime(n::UInt128) =
function isprime(n::UInt128)
depwarn("isprime(...) has been deprecated, use isprime(...) in Primes.jl instead", :isprime)
n <= typemax(UInt64) ? isprime(UInt64(n)) : isprime(BigInt(n))
isprime(n::Int128) = n < 2 ? false :
n <= typemax(Int64) ? isprime(Int64(n)) : isprime(BigInt(n))
end
function isprime(n::Int128)
depwarn("isprime(...) has been deprecated, use isprime(...) in Primes.jl instead", :isprime)
n < 2 ? false : n <= typemax(Int64) ? isprime(Int64(n)) : isprime(BigInt(n))
end


# Trial division of small (< 2^16) precomputed primes +
Expand All @@ -143,6 +161,8 @@ isprime(n::Int128) = n < 2 ? false :
# http://maths-people.anu.edu.au/~brent/pub/pub051.html
#
function factor{T<:Integer}(n::T)
depwarn("factor(...) has been deprecated, use factor(...) in Primes.jl instead", :factor)

0 < n || throw(ArgumentError("number to be factored must be ≥ 0, got $n"))
h = Dict{T,Int}()
n == 1 && return h
Expand Down
3 changes: 3 additions & 0 deletions base/deprecated.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1179,6 +1179,9 @@ end
isequal(x::Char, y::Integer) = false
isequal(x::Integer, y::Char) = false

# prime numbers
include("deprecated-primes.jl")

# During the 0.5 development cycle, do not add any deprecations below this line
# To be deprecated in 0.6

Expand Down
58 changes: 0 additions & 58 deletions base/docs/helpdb/Base.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1291,21 +1291,6 @@ is equivalent to `!isapprox(x,y)`.
"""
isapprox

"""
primes([lo,] hi)
Returns a collection of the prime numbers (from `lo`, if specified) up to `hi`.
"""
primes

"""
primesmask([lo,] hi)
Returns a prime sieve, as a `BitArray`, of the positive integers (from `lo`, if specified)
up to `hi`. Useful when working with either primes or composite numbers.
"""
primesmask

"""
sinh(x)
Expand Down Expand Up @@ -7794,23 +7779,6 @@ the same as the way an object is printed in the Julia REPL.)
"""
TextDisplay

"""
factor(n) -> Dict
Compute the prime factorization of an integer `n`. Returns a dictionary. The keys of the
dictionary correspond to the factors, and hence are of the same type as `n`. The value
associated with each key indicates the number of times the factor appears in the
factorization.
```jldoctest
julia> factor(100) # == 2*2*5*5
Dict{Int64,Int64} with 2 entries:
2 => 2
5 => 2
```
"""
factor

"""
ismatch(r::Regex, s::AbstractString) -> Bool
Expand Down Expand Up @@ -8476,32 +8444,6 @@ any element type for which `dot` is defined), compute the Euclidean dot product
"""
vecdot

"""
isprime(x::Integer) -> Bool
Returns `true` if `x` is prime, and `false` otherwise.
```jldoctest
julia> isprime(3)
true
```
"""
isprime(::Integer)

"""
isprime(x::BigInt, [reps = 25]) -> Bool
Probabilistic primality test. Returns `true` if `x` is prime; and `false` if `x` is not
prime with high probability. The false positive rate is about `0.25^reps`. `reps = 25` is
considered safe for cryptographic applications (Knuth, Seminumerical Algorithms).
```jldoctest
julia> isprime(big(3))
true
```
"""
isprime(::BigInt, ?)

"""
>(x, y)
Expand Down
4 changes: 0 additions & 4 deletions base/exports.jl
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,6 @@ export
exp2,
expm1,
exponent,
factor,
factorial,
fld,
fld1,
Expand Down Expand Up @@ -383,7 +382,6 @@ export
isnan,
isodd,
ispow2,
isprime,
isqrt,
isreal,
isimag,
Expand Down Expand Up @@ -415,8 +413,6 @@ export
prevfloat,
prevpow,
prevpow2,
primes,
primesmask,
rad2deg,
rationalize,
real,
Expand Down
4 changes: 1 addition & 3 deletions base/gmp.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export BigInt

import Base: *, +, -, /, <, <<, >>, >>>, <=, ==, >, >=, ^, (~), (&), (|), ($),
binomial, cmp, convert, div, divrem, factorial, fld, gcd, gcdx, lcm, mod,
ndigits, promote_rule, rem, show, isqrt, string, isprime, powermod,
ndigits, promote_rule, rem, show, isqrt, string, powermod,
sum, trailing_zeros, trailing_ones, count_ones, base, tryparse_internal,
bin, oct, dec, hex, isequal, invmod, prevpow2, nextpow2, ndigits0z, widen, signed, unsafe_trunc, trunc

Expand Down Expand Up @@ -541,8 +541,6 @@ function ndigits0z(x::BigInt, b::Integer=10)
end
ndigits(x::BigInt, b::Integer=10) = x.size == 0 ? 1 : ndigits0z(x,b)

isprime(x::BigInt, reps=25) = ccall((:__gmpz_probab_prime_p,:libgmp), Cint, (Ptr{BigInt}, Cint), &x, reps) > 0

prevpow2(x::BigInt) = x.size < 0 ? -prevpow2(-x) : (x <= 2 ? x : one(BigInt) << (ndigits(x, 2)-1))
nextpow2(x::BigInt) = x.size < 0 ? -nextpow2(-x) : (x <= 2 ? x : one(BigInt) << ndigits(x-1, 2))

Expand Down
6 changes: 0 additions & 6 deletions test/bigint.jl
Original file line number Diff line number Diff line change
Expand Up @@ -217,12 +217,6 @@ g = parse(BigInt,"-1")
@test (|)(a, b, c, d, f) == parse(BigInt,"-1358954753")
@test (|)(a, b, c, d, f, g) == parse(BigInt,"-1")

@test isprime(BigInt(1000000007))
@test isprime(BigInt(1000000007), 1)
@test isprime(BigInt(10000000019))
@test isprime(parse(BigInt,"359334085968622831041960188598043661065388726959079837"))
@test !isprime(BigInt(1))
@test !isprime(BigInt(10000000020))

@test trailing_ones(a) == 8
@test trailing_zeros(b) == 2
Expand Down
9 changes: 0 additions & 9 deletions test/euler.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@ function euler2(n)
end
@test euler2(4000000) == 4613732

#3: 6857
@test maximum(keys(factor(600851475143))) == 6857

#4: 906609
function euler4(n)
m = 1
Expand All @@ -47,10 +44,6 @@ end
#6: 25164150
@test sum(1:100)^2 - sum((1:100).^2) == 25164150

#7: 104743
euler7(n) = primes(floor(Int,n*log(n*log(n))))[n]
@test euler7(10001) == 104743

#8: 40824
function euler8(n,m)
d = digits(n)
Expand All @@ -68,8 +61,6 @@ function euler9(n)
end
@test euler9(1000) == 31875000

#10: 142913828922
@test sum(map(Int64,primes(2000000))) == 142913828922

#11: 70600674
function euler11(grid,n)
Expand Down
Loading

0 comments on commit ab31a15

Please sign in to comment.