Skip to main content
We’ve updated our Terms of Service. A new AI Addendum clarifies how Stack Overflow utilizes AI interactions.
Code Golf

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

Required fields*

LCM of Rational Numbers

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 , so submissions using the fewest bytes win!

Answer*

Draft saved
Draft discarded
Cancel
5
  • \$\begingroup\$ W00t? Try this at your REPL (/ (lcm 1 3 5 7) (gcd 2 4 6 8)). \$\endgroup\$ Commented Jun 19, 2017 at 14:24
  • \$\begingroup\$ @Kaz, since, as it said in problem, "Submissions that would feed rational numbers to an LCM/GCD builtin are allowed, but non-competing". \$\endgroup\$ Commented Jun 19, 2017 at 14:27
  • \$\begingroup\$ In Lisp terms, strictly speaking, we are in fact feeding rationals when we call (lcm 1 3 5 7), since integers are a subtype of rationals, but I think the rule is supposed to exclude use of a lcm or gcd which allows rational inputs. \$\endgroup\$ Commented Jun 19, 2017 at 14:34
  • \$\begingroup\$ @Kaz, ops... I misinterpreted the rules! Should I remove the post? (maybe it is not good marketing for Common Lisp :) \$\endgroup\$ Commented Jun 19, 2017 at 14:52
  • \$\begingroup\$ I'd just put in a note that this is a solution without using the built-in integer lcm and gcd. \$\endgroup\$ Commented Jun 19, 2017 at 14:55

AltStyle によって変換されたページ (->オリジナル) /