18
1
The least common multiple (LCM) of a set of numbers A
is the smallest integer b
such that b/a
is an integer for all integers a
in A
. This definition can be extended to rational numbers!
Task
Find the smallest positive rational b
such that b/a
is an integer for all rationals a
in the input.
Rules
- Standard loopholes are forbidden.
- You may take numerators and denominators separately in the input, but may not take doubles, floats, etc.
- The input may not be fully reduced.
- You may take integer inputs as rationals with denominator of
1
. - Submissions that would feed rational numbers to an LCM/GCD builtin are allowed, but non-competing.
Test Cases
In: 3
Out: 3
In: 1/17
Out: 1/17
In: 1/2, 3/4
Out: 3/2
In: 1/3, 2/8
Out: 1
In: 1/4, 3
Out: 3
In: 2/5, 3
Out: 6
In: 1/2, 3/4, 5/6, 7/8
Out: 105/2
This is code-golf, so submissions using the fewest bytes win!
4Note: computing
LCM[numerators]/GCD[denominators]
may not work when the input contains a non-reduced rational number. e.g.1/3, 2/8
. – JungHwan Min – 2017-06-18T02:26:53.263So if I reduce it, it will work? – Leaky Nun – 2017-06-18T02:32:51.800
@LeakyNun Yes, it will. – JungHwan Min – 2017-06-18T02:34:15.063
To encourage people to submit non-builtin answers, I've edited the question, making builtin answers non-competing (still allowed). If this is a problem, I will rollback my edit. – JungHwan Min – 2017-06-18T09:56:40.270
What about an LCM built-in being used but only with integers - competing or not? – Jonathan Allan – 2017-06-18T10:09:14.730
@JonathanAllan They are competing as long as the inputs of the LCM builtin are only integers. – JungHwan Min – 2017-06-18T10:14:16.750
I wondered at "non-competing but still allowed" but it seems we don't have a rule about that yet, so I've raised it on meta for discussion.
– trichoplax – 2017-06-19T13:30:17.260Maybe include a test case with negative input? – ghosts_in_the_code – 2017-06-21T06:39:28.593