Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

broaden/fix signature of nextprod #35791

Merged
merged 6 commits into from
May 8, 2020
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ Standard library changes
------------------------

* Empty ranges now compare equal, regardless of their startpoint and step ([#32348]).
* The `nextprod` function now accepts tuples and other array types for its first argument ([#35791]).
* A 1-d `Zip` iterator (where `Base.IteratorSize` is `Base.HasShape{1}()`) with defined length of `n` has now also size of `(n,)` (instead of throwing an error with truncated iterators) ([#29927]).
* The `@timed` macro now returns a `NamedTuple` ([#34149]).
* New `supertypes(T)` function returns a tuple of all supertypes of `T` ([#34419]).
Expand Down
10 changes: 5 additions & 5 deletions base/combinatorics.jl
Original file line number Diff line number Diff line change
Expand Up @@ -290,27 +290,27 @@ invperm(P::Any16) = Tuple(invperm(collect(P)))

#XXX This function should be moved to Combinatorics.jl but is currently used by Base.DSP.
"""
nextprod([k_1, k_2,...], n)
nextprod(factors::Union{Tuple,AbstractVector}, n)

Next integer greater than or equal to `n` that can be written as ``\\prod k_i^{p_i}`` for integers
``p_1``, ``p_2``, etc.
``p_1``, ``p_2``, etcetera, for factors ``k_i`` in `factors`.

# Examples
```jldoctest
julia> nextprod([2, 3], 105)
julia> nextprod((2, 3), 105)
108

julia> 2^2 * 3^3
108
```
"""
stevengj marked this conversation as resolved.
Show resolved Hide resolved
function nextprod(a::Vector{Int}, x)
function nextprod(a::Union{Tuple{Vararg{<:Integer}},AbstractVector{<:Integer}}, x::Real)
if x > typemax(Int)
throw(ArgumentError("unsafe for x > typemax(Int), got $x"))
end
k = length(a)
v = fill(1, k) # current value of each counter
mx = [nextpow(ai,x) for ai in a] # maximum value of each counter
mx = map(a -> nextpow(a,x), a) # maximum value of each counter
v[1] = mx[1] # start at first case that is >= x
p::widen(Int) = mx[1] # initial value of product in this case
best = p
Expand Down
3 changes: 2 additions & 1 deletion test/numbers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2013,8 +2013,9 @@ end
end
@testset "nextprod" begin
@test_throws ArgumentError nextprod([2,3,5],Int128(typemax(Int))+1)
@test nextprod([2,3,5],30) == 30
@test nextprod([2,3,5],30) == nextprod((2,3,5),30) == 30
@test nextprod([2,3,5],33) == 36
@test nextprod([3,5],33) == nextprod(3:2:5,33) == 45
end
@testset "nextfloat/prevfloat" begin
@test nextfloat(0.0) == 5.0e-324
Expand Down