Talk:Least common multiple: Difference between revisions

From Rosetta Code
Content added Content deleted
(When I first wrote the task, I forgot about the special case for zero. I later added this special case to the task, but I left it as a draft task. --~~~~)
 
Line 1: Line 1:
When I first wrote the task, I forgot about the special case for zero. I later added this special case to the task, but I left it as a draft task. --[[User:Kernigh|Kernigh]] 00:29, 31 March 2011 (UTC)
When I first wrote the task, I forgot about the special case for zero. I later added this special case to the task, but I left it as a draft task. --[[User:Kernigh|Kernigh]] 00:29, 31 March 2011 (UTC)

==Yet another way, isn't?==
From ''"Yet another way is to use rational arithmetic; if you add 1/m and 1/n and reduce the fraction, then the denominator is the lcm"''

I did the following:
<lang python>>>> def lcm(a, b): return (fractions.Fraction(1,a) + fractions.Fraction(1,b)).denominator if a and b else 0

>>> lcm(12, 18)
36
>>> lcm(-6, 14)
21
>>> # Whoops!</lang>
It is out by any common factor. --[[User:Paddy3118|Paddy3118]] 03:17, 31 March 2011 (UTC)

Revision as of 03:17, 31 March 2011

When I first wrote the task, I forgot about the special case for zero. I later added this special case to the task, but I left it as a draft task. --Kernigh 00:29, 31 March 2011 (UTC)

Yet another way, isn't?

From "Yet another way is to use rational arithmetic; if you add 1/m and 1/n and reduce the fraction, then the denominator is the lcm"

I did the following: <lang python>>>> def lcm(a, b): return (fractions.Fraction(1,a) + fractions.Fraction(1,b)).denominator if a and b else 0

>>> lcm(12, 18) 36 >>> lcm(-6, 14) 21 >>> # Whoops!</lang> It is out by any common factor. --Paddy3118 03:17, 31 March 2011 (UTC)