diff --git a/NEWS.md b/NEWS.md index 5c7187a1853532..25c513d1a5a302 100644 --- a/NEWS.md +++ b/NEWS.md @@ -35,7 +35,7 @@ New library features Standard library changes ------------------------ - +* The `nextprod` function now accepts tuples and other array types for its first argument ([#35791]). #### LinearAlgebra diff --git a/base/combinatorics.jl b/base/combinatorics.jl index 3e16c457223fd4..04080000507103 100644 --- a/base/combinatorics.jl +++ b/base/combinatorics.jl @@ -290,27 +290,30 @@ 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 ``` + +!!! compat "Julia 1.6" + The method that accepts a tuple requires Julia 1.6 or later. """ -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 diff --git a/test/numbers.jl b/test/numbers.jl index bbe97017bde68d..a1ab3a53f6a634 100644 --- a/test/numbers.jl +++ b/test/numbers.jl @@ -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