From c7cd31d23cd3110a8e13599127273060e3ce58d9 Mon Sep 17 00:00:00 2001 From: cyanescent <161313975+cyanescent@users.noreply.github.com> Date: Fri, 11 Oct 2024 17:57:43 +0200 Subject: [PATCH 1/2] Correction of the gcd() and lcm() of Array The init argument in the reduce() function was incorrect. It is sufficient to initialize with any value in the Array. --- base/intfuncs.jl | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/base/intfuncs.jl b/base/intfuncs.jl index ec450aff2dff2..9aaba0b8e0dda 100644 --- a/base/intfuncs.jl +++ b/base/intfuncs.jl @@ -36,6 +36,9 @@ julia> gcd(1//3, 2) julia> gcd(0, 0, 10, 15) 5 + +julia> gcd([1//3; 2//3]) +1//3 ``` """ function gcd(a::T, b::T) where T<:Integer @@ -125,6 +128,9 @@ julia> lcm(1//3, 2) julia> lcm(1, 3, 5, 7) 105 + +julia> lcm([1//3; 2//3]) +2//3 ``` """ function lcm(a::T, b::T) where T<:Integer @@ -149,8 +155,8 @@ lcm(a::Real, b::Real, c::Real...) = lcm(a, lcm(b, c...)) gcd(a::T, b::T) where T<:Real = throw(MethodError(gcd, (a,b))) lcm(a::T, b::T) where T<:Real = throw(MethodError(lcm, (a,b))) -gcd(abc::AbstractArray{<:Real}) = reduce(gcd, abc; init=zero(eltype(abc))) -lcm(abc::AbstractArray{<:Real}) = reduce(lcm, abc; init=one(eltype(abc))) +gcd(abc::AbstractArray{<:Real}) = reduce(gcd, abc; init=abc[1]) +lcm(abc::AbstractArray{<:Real}) = reduce(lcm, abc; init=abc[1]) function gcd(abc::AbstractArray{<:Integer}) a = zero(eltype(abc)) From 4e8226052a01f1c19d80bc74c0a515e4349598e2 Mon Sep 17 00:00:00 2001 From: cyanescent <161313975+cyanescent@users.noreply.github.com> Date: Tue, 15 Oct 2024 01:56:17 +0200 Subject: [PATCH 2/2] Simpler correction with better error Co-authored-by: Neven Sajko --- base/intfuncs.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/base/intfuncs.jl b/base/intfuncs.jl index 9aaba0b8e0dda..7ac1218bc0df6 100644 --- a/base/intfuncs.jl +++ b/base/intfuncs.jl @@ -155,8 +155,8 @@ lcm(a::Real, b::Real, c::Real...) = lcm(a, lcm(b, c...)) gcd(a::T, b::T) where T<:Real = throw(MethodError(gcd, (a,b))) lcm(a::T, b::T) where T<:Real = throw(MethodError(lcm, (a,b))) -gcd(abc::AbstractArray{<:Real}) = reduce(gcd, abc; init=abc[1]) -lcm(abc::AbstractArray{<:Real}) = reduce(lcm, abc; init=abc[1]) +gcd(abc::AbstractArray{<:Real}) = reduce(gcd, abc) +lcm(abc::AbstractArray{<:Real}) = reduce(lcm, abc) function gcd(abc::AbstractArray{<:Integer}) a = zero(eltype(abc))