Maurice Fonk

Greatest Common Divisor and Least Common Multiple in CoffeeScript

Greatest Common Divisor and Least Common Multiple in CoffeeScript

Very useful when working on Project Euler.

Greatest Common Divisor

# Greatest common divisor of a and b
greatestCommonDivisor = (a, b) ->
  while b isnt 0
    t = b
    b = a %% b
    a = t

  a

Least Common Multiple

Uses greatestCommonDivisor()

# Least common multiple of a and b
leastCommonMultiple = (a, b) -> (a * b) // greatestCommonDivisor a, b