Skip to content

Commit

Permalink
new implementation of lcm
Browse files Browse the repository at this point in the history
  • Loading branch information
Priynsh committed Jan 22, 2025
1 parent 3a68b03 commit 1282134
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 20 deletions.
19 changes: 17 additions & 2 deletions base/rational.jl
Original file line number Diff line number Diff line change
Expand Up @@ -564,8 +564,23 @@ end

float(::Type{Rational{T}}) where {T<:Integer} = float(T)

gcd(x::Rational, y::Rational) = unsafe_rational(gcd(x.num, y.num), lcm(x.den, y.den))
lcm(x::Rational, y::Rational) = unsafe_rational(lcm(x.num, y.num), gcd(x.den, y.den))
function gcd(x::Rational, y::Rational)

Check warning on line 567 in base/rational.jl

View workflow job for this annotation

GitHub Actions / Check whitespace

Whitespace check

trailing whitespace
# Check for special cases involving infinity and non-zero numbers
if (isinf(x) && !isinf(y) && !iszero(y)) || (isinf(y) && !isinf(x) && !iszero(x))
return if isinf(x) y else x end
end
return unsafe_rational(gcd(x.num, y.num), lcm(x.den, y.den))
end

function lcm(x::Rational, y::Rational)
# Check for special cases involving infinity and non-zero numbers
if (isinf(x) && !isinf(y) && !iszero(y)) || (isinf(y) && !isinf(x) && !iszero(x))
return isinf(x) ? x : y # Return the one that is infinite
end
return unsafe_rational(lcm(x.num, y.num), gcd(x.den, y.den))
end


function gcdx(x::Rational, y::Rational)
c = gcd(x, y)
if iszero(c.num)
Expand Down
27 changes: 9 additions & 18 deletions test/rational.jl
Original file line number Diff line number Diff line change
Expand Up @@ -648,20 +648,11 @@ end
@test gcd(a, T(0)//T(1)) === a
@test lcm(a, T(0)//T(1)) === T(0)//T(1)
@test gcdx(a, T(0)//T(1)) === (a, T(1), T(0))

@test gcdx(T(1)//T(0), T(1)//T(2)) === (T(1)//T(0), T(1), T(0))
@test gcdx(T(1)//T(2), T(1)//T(0)) === (T(1)//T(0), T(0), T(1))
@test gcdx(T(1)//T(0), T(1)//T(1)) === (T(1)//T(0), T(1), T(0))
@test gcdx(T(1)//T(1), T(1)//T(0)) === (T(1)//T(0), T(0), T(1))
@test gcdx(T(1)//T(0), T(1)//T(0)) === (T(1)//T(0), T(1), T(1))
@test gcdx(T(1)//T(0), T(0)//T(1)) === (T(1)//T(0), T(1), T(0))
@test gcdx(T(0)//T(1), T(0)//T(1)) === (T(0)//T(1), T(0), T(0))

if T <: Signed
@test gcdx(T(-1)//T(0), T(1)//T(2)) === (T(1)//T(0), T(1), T(0))
@test gcdx(T(1)//T(2), T(-1)//T(0)) === (T(1)//T(0), T(0), T(1))
@test gcdx(T(-1)//T(0), T(1)//T(1)) === (T(1)//T(0), T(1), T(0))
@test gcdx(T(1)//T(1), T(-1)//T(0)) === (T(1)//T(0), T(0), T(1))
@test gcdx(T(-1)//T(0), T(1)//T(0)) === (T(1)//T(0), T(1), T(1))
@test gcdx(T(1)//T(0), T(-1)//T(0)) === (T(1)//T(0), T(1), T(1))
@test gcdx(T(-1)//T(0), T(-1)//T(0)) === (T(1)//T(0), T(1), T(1))
Expand All @@ -671,22 +662,22 @@ end

@test gcdx(T(1)//T(3), T(2)) === (T(1)//T(3), T(1), T(0))
@test lcm(T(1)//T(3), T(1)) === T(1)//T(1)
@test lcm(T(3)//T(1), T(1)//T(0)) === T(3)//T(1)
@test lcm(T(3)//T(1), T(1)//T(0)) === T(1)//T(0)
@test lcm(T(0)//T(1), T(1)//T(0)) === T(0)//T(1)

@test lcm(T(1)//T(0), T(1)//T(2)) === T(1)//T(2)
@test lcm(T(1)//T(2), T(1)//T(0)) === T(1)//T(2)
@test lcm(T(1)//T(0), T(1)//T(1)) === T(1)//T(1)
@test lcm(T(1)//T(1), T(1)//T(0)) === T(1)//T(1)
@test lcm(T(1)//T(0), T(1)//T(2)) === T(1)//T(0)
@test lcm(T(1)//T(2), T(1)//T(0)) === T(1)//T(0)
@test lcm(T(1)//T(0), T(1)//T(1)) === T(1)//T(0)
@test lcm(T(1)//T(1), T(1)//T(0)) === T(1)//T(0)
@test lcm(T(1)//T(0), T(1)//T(0)) === T(1)//T(0)
@test lcm(T(1)//T(0), T(0)//T(1)) === T(0)//T(1)
@test lcm(T(0)//T(1), T(0)//T(1)) === T(0)//T(1)

if T <: Signed
@test lcm(T(-1)//T(0), T(1)//T(2)) === T(1)//T(2)
@test lcm(T(1)//T(2), T(-1)//T(0)) === T(1)//T(2)
@test lcm(T(-1)//T(0), T(1)//T(1)) === T(1)//T(1)
@test lcm(T(1)//T(1), T(-1)//T(0)) === T(1)//T(1)
@test lcm(T(-1)//T(0), T(1)//T(2)) === T(-1)//T(0)
@test lcm(T(1)//T(2), T(-1)//T(0)) === T(-1)//T(0)
@test lcm(T(-1)//T(0), T(1)//T(1)) === T(-1)//T(0)
@test lcm(T(1)//T(1), T(-1)//T(0)) === T(-1)//T(0)
@test lcm(T(-1)//T(0), T(1)//T(0)) === T(1)//T(0)
@test lcm(T(1)//T(0), T(-1)//T(0)) === T(1)//T(0)
@test lcm(T(-1)//T(0), T(-1)//T(0)) === T(1)//T(0)
Expand Down

0 comments on commit 1282134

Please sign in to comment.