Talk:Least common multiple: Difference between revisions

From Rosetta Code
Content added Content deleted
(→‎Java solution: new section)
Line 22: Line 22:


::I have not been able to find a good online reference on LCM that treats negative arguments consistently (for example, consider the distributive and idempotent properties described at the mathworld reference). But 1/6 + 1/14 in reduced form is 5/21 where the least common multiple of 6 and 14 is 42. --[[User:Rdm|Rdm]] 15:35, 31 March 2011 (UTC)
::I have not been able to find a good online reference on LCM that treats negative arguments consistently (for example, consider the distributive and idempotent properties described at the mathworld reference). But 1/6 + 1/14 in reduced form is 5/21 where the least common multiple of 6 and 14 is 42. --[[User:Rdm|Rdm]] 15:35, 31 March 2011 (UTC)

== Java solution ==

I suspect the intention of the java code before my edits was<lang java>for (i = MultipleOfN/m; ...)</lang> instead of <lang java>for (i = MultipleOfM/m; ...)</lang>, where the former makes somewhat more sense. --[[User:Ledrug|Ledrug]] 16:25, 22 August 2011 (UTC)

Revision as of 16:25, 22 August 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)

I do not know where you found that sentence, but a bigger problem seems to be that all of the page history for this page displays for me as 2011-03-11 when I believe this page has been around for quite some time. --Rdm 13:03, 31 March 2011 (UTC)
That sentence used to be in the description, but it was removed when it was found to be wrong. Also the page is even younger than that (as long as I'm reading your date format correctly) It was only created yesterday. I was amazed too. I thought for sure we had this one, but I couldn't find it. --Mwn3d 13:31, 31 March 2011 (UTC)
Hi Rdm, try this direct link. --Paddy3118 13:35, 31 March 2011 (UTC)
Does lcm(x,y) = lcm(-x,y) = lcm(-x,-y)? If so then you could just change the sign on the arguments and use the fractional method. I'm pretty sure it works for positive numbers. The fractional method wouldn't work for fractional arguments, though (ex: lcm(1/3, 1/6) = 1/3 = 1 * 1/3 = 2 * 1/6). For those you would nee to use the "iteration over multiples" method I believe. --Mwn3d 14:53, 31 March 2011 (UTC)
I have not been able to find a good online reference on LCM that treats negative arguments consistently (for example, consider the distributive and idempotent properties described at the mathworld reference). But 1/6 + 1/14 in reduced form is 5/21 where the least common multiple of 6 and 14 is 42. --Rdm 15:35, 31 March 2011 (UTC)

Java solution

I suspect the intention of the java code before my edits was<lang java>for (i = MultipleOfN/m; ...)</lang> instead of <lang java>for (i = MultipleOfM/m; ...)</lang>, where the former makes somewhat more sense. --Ledrug 16:25, 22 August 2011 (UTC)