Skip to content

Instantly share code, notes, and snippets.

@danielrose7
Created September 11, 2016 07:31
Show Gist options
  • Save danielrose7/9de823c66e8829fcffaa5fa4e259f8fb to your computer and use it in GitHub Desktop.
Save danielrose7/9de823c66e8829fcffaa5fa4e259f8fb to your computer and use it in GitHub Desktop.
Given the lengths of two different tracks, output how many laps it will take until two joggers meet again
# lcm => least common multiple
# one-liner
def nbr_of_laps(x,y)
[x.lcm(y)/x, x.lcm(y)/y]
end
# simple
def nbr_of_laps(x, y)
lcm = x.lcm(y)
[lcm/x, lcm/y]
end
# alt
def nbr_of_laps(x,y)
l = [x,y].reduce(:lcm)
[l/x,l/y]
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment