Least common multiple: Difference between revisions

→‎{{header|Standard ML}}: add another SML version
(→‎{{header|Standard ML}}: add another SML version)
 
(12 intermediate revisions by 9 users not shown)
Line 40:
 
=={{header|11l}}==
<langsyntaxhighlight lang="11l">F gcd(=a, =b)
L b != 0
(a, b) = (b, a % b)
Line 48:
R m I/ gcd(m, n) * n
 
print(lcm(12, 18))</langsyntaxhighlight>
 
{{out}}
Line 59:
For maximum compatibility, this program uses only the basic instruction set (S/360)
with 2 ASSIST macros (XDECO,XPRNT).
<langsyntaxhighlight lang="360asm">LCM CSECT
USING LCM,R15 use calling register
L R6,A a
Line 88:
XDEC DS CL12 temp for edit
YREGS
END LCM</langsyntaxhighlight>
{{out}}
<pre>
Line 95:
 
=={{header|8th}}==
<langsyntaxhighlight lang="forth">
: gcd \ a b -- gcd
dup 0 n:= if drop ;; then
Line 120:
 
 
bye</langsyntaxhighlight>
{{out}}
<pre>LCM of 18 and 12 = 36
Line 128:
 
=={{header|Action!}}==
<langsyntaxhighlight Actionlang="action!">CARD FUNC Lcm(CARD a,b)
CARD tmp,c
 
Line 159:
Test(1,56)
Test(12,0)
RETURN</langsyntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Least_common_multiple.png Screenshot from Atari 8-bit computer]
Line 172:
=={{header|Ada}}==
lcm_test.adb:
<langsyntaxhighlight Adalang="ada">with Ada.Text_IO; use Ada.Text_IO;
 
procedure Lcm_Test is
Line 199:
Put_Line ("LCM of -6, 14 is" & Integer'Image (Lcm (-6, 14)));
Put_Line ("LCM of 35, 0 is" & Integer'Image (Lcm (35, 0)));
end Lcm_Test;</langsyntaxhighlight>
 
Output:
Line 207:
 
=={{header|ALGOL 68}}==
<langsyntaxhighlight lang="algol68">
BEGIN
PROC gcd = (INT m, n) INT :
Line 223:
"and their greatest common divisor is", gcd(m,n)))
END
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 233:
 
=={{header|ALGOL W}}==
<langsyntaxhighlight lang="algolw">begin
integer procedure gcd ( integer value a, b ) ;
if b = 0 then a else gcd( b, a rem abs(b) );
Line 241:
 
write( lcm( 15, 20 ) );
end.</langsyntaxhighlight>
 
=={{header|APL}}==
APL provides this function.
<langsyntaxhighlight lang="apl"> 12^18
36</langsyntaxhighlight>
If for any reason we wanted to reimplement it, we could do so in terms of the greatest common divisor by transcribing the formula set out in the task specification into APL notation:
<langsyntaxhighlight lang="apl"> LCM←{(|⍺×⍵)÷⍺∨⍵}
12 LCM 18
36</langsyntaxhighlight>
 
=={{header|AppleScript}}==
 
<langsyntaxhighlight AppleScriptlang="applescript">------------------ LEAST COMMON MULTIPLE -----------------
 
-- lcm :: Integral a => a -> a -> a
Line 300:
result's |λ|(abs(x), abs(y))
end gcd</langsyntaxhighlight>
{{Out}}
<syntaxhighlight lang="applescript">36</syntaxhighlight>
<lang AppleScript>36</lang>
 
=={{header|Arendelle}}==
Line 318:
 
=={{header|Arturo}}==
<langsyntaxhighlight lang="rebol">lcm: function [x,y][
x * y / gcd @[x y]
]
 
print lcm 12 18</langsyntaxhighlight>
{{out}}
Line 330:
=={{header|Assembly}}==
==={{header|x86 Assembly}}===
<langsyntaxhighlight lang="asm">
; lcm.asm: calculates the least common multiple
; of two positive integers
Line 422:
ret
 
</syntaxhighlight>
</lang>
 
=={{header|ATS}}==
Line 428:
Compile with ‘patscc -o lcm lcm.dats’
 
<langsyntaxhighlight lang="ats">#define ATS_DYNLOADFLAG 0 (* No initialization is needed. *)
 
#include "share/atspre_define.hats"
Line 529:
assertloc (lcm (12, 22) = 132ULL);
assertloc (lcm (7ULL, 31ULL) = 217ULL)
end</langsyntaxhighlight>
 
=={{header|AutoHotkey}}==
<langsyntaxhighlight lang="autohotkey">LCM(Number1,Number2)
{
If (Number1 = 0 || Number2 = 0)
Line 544:
Num1 = 12
Num2 = 18
MsgBox % LCM(Num1,Num2)</langsyntaxhighlight>
 
=={{header|AutoIt}}==
<syntaxhighlight lang="autoit">
<lang AutoIt>
Func _LCM($a, $b)
Local $c, $f, $m = $a, $n = $b
Line 561:
Return $m * $n / $b
EndFunc ;==>_LCM
</syntaxhighlight>
</lang>
Example
<syntaxhighlight lang="autoit">
<lang AutoIt>
ConsoleWrite(_LCM(12,18) & @LF)
ConsoleWrite(_LCM(-5,12) & @LF)
ConsoleWrite(_LCM(13,0) & @LF)
</syntaxhighlight>
</lang>
<pre>
36
Line 576:
 
=={{header|AWK}}==
<langsyntaxhighlight lang="awk"># greatest common divisor
function gcd(m, n, t) {
# Euclid's method
Line 597:
# Read two integers from each line of input.
# Print their least common multiple.
{ print lcm($1, $2) }</langsyntaxhighlight>
 
Example input and output: <pre>$ awk -f lcd.awk
Line 612:
==={{header|Applesoft BASIC}}===
ported from BBC BASIC
<langsyntaxhighlight ApplesoftBasiclang="applesoftbasic">10 DEF FN MOD(A) = INT((A / B - INT(A / B)) * B + .05) * SGN(A / B)
20 INPUT"M=";M%
30 INPUT"N=";N%
Line 633:
250 NEXT B
260 R = ABS(A%)
270 RETURN</langsyntaxhighlight>
 
==={{header|BBC BASIC}}===
{{Works with|BBC BASIC for Windows}}
<syntaxhighlight lang="bbc basic">
<lang BBC BASIC>
DEF FN_LCM(M%,N%)
IF M%=0 OR N%=0 THEN =0 ELSE =ABS(M%*N%)/FN_GCD_Iterative_Euclid(M%, N%)
Line 649:
ENDWHILE
= ABS(A%)
</syntaxhighlight>
</lang>
 
==={{header|IS-BASIC}}===
<langsyntaxhighlight ISlang="is-BASICbasic">100 DEF LCM(A,B)=(A*B)/GCD(A,B)
110 DEF GCD(A,B)
120 DO WHILE B>0
Line 659:
150 LET GCD=A
160 END DEF
170 PRINT LCM(12,18)</langsyntaxhighlight>
 
==={{header|Tiny BASIC}}===
{{works with|TinyBasic}}
<lang Tiny BASIC>10 PRINT "First number"
<syntaxhighlight lang="basic">10 PRINT "First number"
20 INPUT A
30 PRINT "Second number"
Line 681 ⟶ 682:
140 LET Q=R
150 LET R=C
160 GOTO 70</langsyntaxhighlight>
 
=={{header|BASIC256}}==
===Iterative solution===
<langsyntaxhighlight BASIC256lang="basic256">function mcm (m, n)
if m = 0 or n = 0 then return 0
if m < n then
Line 700 ⟶ 701:
print "lcm( 15, 12) = "; mcm( 15, 12)
print "lcm(-10, -14) = "; mcm(-10, -14)
print "lcm( 0, 1) = "; mcm( 0, 1)</langsyntaxhighlight>
{{out}}
<pre>lcm( 12, 18) = 36
Line 710 ⟶ 711:
===Recursive solution===
Reuses code from Greatest_common_divisor#Recursive_solution and correctly handles negative arguments
<langsyntaxhighlight BASIC256lang="basic256">function gcdp(a, b)
if b = 0 then return a
return gcdp(b, a mod b)
Line 726 ⟶ 727:
print "lcm( 15, 12) = "; lcm( 15, 12)
print "lcm(-10, -14) = "; lcm(-10, -14)
print "lcm( 0, 1) = "; lcm( 0, 1)</langsyntaxhighlight>
{{out}}
<pre>lcm( 12, -18) = 36.0
Line 734 ⟶ 735:
 
=={{header|Batch File}}==
<langsyntaxhighlight lang="dos">@echo off
setlocal enabledelayedexpansion
set num1=12
Line 751 ⟶ 752:
set /a res = %1 %% %2
call :lcm %2 %res%
goto :EOF</langsyntaxhighlight>
{{Out}}
<pre>LCM = 36</pre>
Line 757 ⟶ 758:
=={{header|bc}}==
{{trans|AWK}}
<langsyntaxhighlight lang="bc">/* greatest common divisor */
define g(m, n) {
auto t
Line 778 ⟶ 779:
if (r < 0) return (-r)
return (r)
}</langsyntaxhighlight>
 
=={{header|BCPL}}==
<langsyntaxhighlight lang="bcpl">get "libhdr"
 
let lcm(m,n) =
Line 791 ⟶ 792:
gcd(n, m rem n)
 
let start() be writef("%N*N", lcm(12, 18))</langsyntaxhighlight>
{{out}}
<pre>36</pre>
Line 799 ⟶ 800:
Inputs are limited to signed 16-bit integers.
 
<langsyntaxhighlight lang="befunge">&>:0`2*1-*:&>:#@!#._:0`2*1v
>28*:*:**+:28*>:*:*/\:vv*-<
|<:%/*:*:*82\%*:*:*82<<>28v
>$/28*:*:*/*.@^82::+**:*:*<</langsyntaxhighlight>
 
{{in}}
Line 812 ⟶ 813:
 
=={{header|BQN}}==
<langsyntaxhighlight lang="bqn">Lcm ← ×÷{𝕨(|𝕊⍟(>⟜0)⊣)𝕩}</langsyntaxhighlight>
 
Example:
 
<syntaxhighlight lang ="bqn">12 Lcm 18</langsyntaxhighlight>
<pre>36</pre>
 
=={{header|Bracmat}}==
We utilize the fact that Bracmat simplifies fractions (using Euclid's algorithm). The function <code>den$<i>number</i></code> returns the denominator of a number.
<langsyntaxhighlight lang="bracmat">(gcd=
a b
. !arg:(?a.?b)
Line 828 ⟶ 829:
* !a
);
out$(gcd$(12.18) gcd$(-6.14) gcd$(35.0) gcd$(117.18))</langsyntaxhighlight>
Output:
<pre>36 42 35 234</pre>
 
=={{header|Brat}}==
<langsyntaxhighlight lang="brat">
gcd = { a, b |
true? { a == 0 }
Line 846 ⟶ 847:
p lcm(12, 18) # 36
p lcm(14, 21) # 42
</syntaxhighlight>
</lang>
 
=={{header|Bruijn}}==
{{trans|Haskell}}
<syntaxhighlight lang="bruijn">
:import std/Math .
 
lcm [[=?1 1 (=?0 0 |(1 / (gcd 1 0) ⋅ 0))]]
 
:test ((lcm (+12) (+18)) =? (+36)) ([[1]])
:test ((lcm (+42) (+25)) =? (+1050)) ([[1]])
</syntaxhighlight>
 
=={{header|C}}==
<langsyntaxhighlight Clang="c">#include <stdio.h>
 
int gcd(int m, int n)
Line 867 ⟶ 879:
printf("lcm(35, 21) = %d\n", lcm(21,35));
return 0;
}</langsyntaxhighlight>
 
=={{header|C sharp|C#}}==
<langsyntaxhighlight lang="csharp">Using System;
class Program
{
Line 886 ⟶ 898:
}
}
</syntaxhighlight>
</lang>
{{out}}
<pre>lcm(12,18)=36</pre>
Line 892 ⟶ 904:
=={{header|C++}}==
{{libheader|Boost}}
<langsyntaxhighlight lang="cpp">#include <boost/math/common_factor.hpp>
#include <iostream>
 
Line 900 ⟶ 912:
<< "and the greatest common divisor " << boost::math::gcd( 12 , 18 ) << " !" << std::endl ;
return 0 ;
}</langsyntaxhighlight>
 
{{out}}
Line 909 ⟶ 921:
=== Alternate solution ===
{{works with|C++11}}
<langsyntaxhighlight lang="cpp">
#include <cstdlib>
#include <iostream>
Line 934 ⟶ 946:
return 0;
}
</syntaxhighlight>
</lang>
 
=={{header|Clojure}}==
<langsyntaxhighlight Clojurelang="clojure">(defn gcd
[a b]
(if (zero? b)
Line 948 ⟶ 960:
;; to calculate the lcm for a variable number of arguments
(defn lcmv [& v] (reduce lcm v))
</syntaxhighlight>
</lang>
 
=={{header|CLU}}==
<langsyntaxhighlight lang="clu">gcd = proc (m, n: int) returns (int)
m, n := int$abs(m), int$abs(n)
while n ~= 0 do m, n := n, m // n end
Line 967 ⟶ 979:
po: stream := stream$primary_output()
stream$putl(po, int$unparse(lcm(12, 18)))
end start_up</langsyntaxhighlight>
{{out}}
<pre>36</pre>
 
=={{header|COBOL}}==
<langsyntaxhighlight lang="cobol"> IDENTIFICATION DIVISION.
PROGRAM-ID. show-lcm.
 
Line 1,034 ⟶ 1,046:
GOBACK
.
END FUNCTION gcd.</langsyntaxhighlight>
 
=={{header|Common Lisp}}==
Common Lisp provides the <tt>lcm</tt> function. It can accept two or more (or less) parameters.
 
<langsyntaxhighlight lang="lisp">CL-USER> (lcm 12 18)
36
CL-USER> (lcm 12 18 22)
396</langsyntaxhighlight>
 
Here is one way to reimplement it.
 
<langsyntaxhighlight lang="lisp">CL-USER> (defun my-lcm (&rest args)
(reduce (lambda (m n)
(cond ((or (= m 0) (= n 0)) 0)
Line 1,055 ⟶ 1,067:
36
CL-USER> (my-lcm 12 18 22)
396</langsyntaxhighlight>
 
In this code, the <tt>lambda</tt> finds the least common multiple of two integers, and the <tt>reduce</tt> transforms it to accept any number of parameters. The <tt>reduce</tt> operation exploits how ''lcm'' is associative, <tt>(lcm a b c) == (lcm (lcm a b) c)</tt>; and how 1 is an identity, <tt>(lcm 1 a) == a</tt>.
 
=={{header|Cowgol}}==
<langsyntaxhighlight lang="cowgol">include "cowgol.coh";
 
sub gcd(m: uint32, n: uint32): (r: uint32) is
Line 1,080 ⟶ 1,092:
 
print_i32(lcm(12, 18));
print_nl();</langsyntaxhighlight>
{{out}}
<pre>36</pre>
 
=={{header|D}}==
<langsyntaxhighlight lang="d">import std.stdio, std.bigint, std.math;
 
T gcd(T)(T a, T b) pure nothrow {
Line 1,106 ⟶ 1,118:
lcm("2562047788015215500854906332309589561".BigInt,
"6795454494268282920431565661684282819".BigInt).writeln;
}</langsyntaxhighlight>
{{out}}
<pre>36
Line 1,112 ⟶ 1,124:
 
=={{header|Dart}}==
<langsyntaxhighlight lang="dart">
main() {
int x=8;
Line 1,129 ⟶ 1,141:
}
</syntaxhighlight>
</lang>
=={{header|Delphi}}==
See [https://rosettacode.org/wiki/Least_common_multiple#Pascal Pascal].
 
=={{header|DWScript}}==
<langsyntaxhighlight lang="delphi">PrintLn(Lcm(12, 18));</langsyntaxhighlight>
Output:
<pre>36</pre>
 
=={{header|Draco}}==
<langsyntaxhighlight lang="draco">proc gcd(word m, n) word:
word t;
while n /= 0 do
Line 1,158 ⟶ 1,170:
proc main() void:
writeln(lcm(12, 18))
corp</langsyntaxhighlight>
{{out}}
<pre>36</pre>
 
=={{header|EasyLang}}==
<syntaxhighlight>
func gcd a b .
while b <> 0
h = b
b = a mod b
a = h
.
return a
.
func lcm a b .
return a / gcd a b * b
.
print lcm 12 18
</syntaxhighlight>
{{out}}
<pre>
36
</pre>
 
=={{header|EchoLisp}}==
(lcm a b) is already here as a two arguments function. Use foldl to find the lcm of a list of numbers.
<langsyntaxhighlight lang="lisp">
(lcm 0 9) → 0
(lcm 444 888)→ 888
Line 1,171 ⟶ 1,203:
(define (lcm* list) (foldl lcm (first list) list)) → lcm*
(lcm* '(444 888 999)) → 7992
</syntaxhighlight>
</lang>
 
=={{header|Elena}}==
{{trans|C#}}
ELENA 46.x :
<langsyntaxhighlight lang="elena">import extensions;
import system'math;
gcd = (m,n => (n == 0) ? (m.Absolute) : (gcd(n,n.mod:(m))));
lcm = (m,n => (m * n).Absolute / gcd(m,n));
Line 1,186 ⟶ 1,218:
{
console.printLine("lcm(12,18)=",lcm(12,18))
}</langsyntaxhighlight>
{{out}}
<pre>
Line 1,193 ⟶ 1,225:
 
=={{header|Elixir}}==
<langsyntaxhighlight lang="elixir">defmodule RC do
def gcd(a,0), do: abs(a)
def gcd(a,b), do: gcd(b, rem(a,b))
Line 1,200 ⟶ 1,232:
end
 
IO.puts RC.lcm(-12,15)</langsyntaxhighlight>
 
{{out}}
Line 1,208 ⟶ 1,240:
 
=={{header|Erlang}}==
<langsyntaxhighlight lang="erlang">% Implemented by Arjun Sunel
-module(lcm).
-export([main/0]).
Line 1,222 ⟶ 1,254:
 
lcm(A,B) ->
abs(A*B div gcd(A,B)).</langsyntaxhighlight>
 
{{out}}
Line 1,229 ⟶ 1,261:
 
=={{header|ERRE}}==
<langsyntaxhighlight ERRElang="erre">PROGRAM LCM
 
PROCEDURE GCD(A,B->GCD)
Line 1,258 ⟶ 1,290:
LCM(0,35->LCM)
PRINT("LCM of 0 AND 35 =";LCM)
END PROGRAM</langsyntaxhighlight>
 
{{out}}
Line 1,265 ⟶ 1,297:
LCM of 0 and 35 = 0
</pre>
 
=={{header|Euler}}==
Note % is integer division in Euler, not the mod operator.
 
'''begin'''
'''new''' gcd; '''new''' lcm;
gcd <- ` '''formal''' a; '''formal''' b;
'''if''' b = 0 '''then''' a '''else''' gcd( b, a '''mod''' '''abs''' b )
'
;
lcm <- ` '''formal''' a; '''formal''' b;
'''abs''' [ a * b ] % gcd( a, b )
'
;
'''out''' lcm( 15, 20 )
'''end''' $
 
=={{header|Euphoria}}==
<langsyntaxhighlight lang="euphoria">function gcd(integer m, integer n)
integer tmp
while m do
Line 1,279 ⟶ 1,328:
function lcm(integer m, integer n)
return m / gcd(m, n) * n
end function</langsyntaxhighlight>
 
=={{header|Excel}}==
Excel's LCM can handle multiple values. Type in a cell:
<langsyntaxhighlight lang="excel">=LCM(A1:J1)</langsyntaxhighlight>
This will get the LCM on the first 10 cells in the first row. Thus :
<pre>12 3 5 23 13 67 15 9 4 2
Line 1,290 ⟶ 1,339:
 
=={{header|Ezhil}}==
<langsyntaxhighlight lang="src="Ezhilezhil"">
## இந்த நிரல் இரு எண்களுக்கு இடையிலான மீச்சிறு பொது மடங்கு (LCM), மீப்பெரு பொது வகுத்தி (GCD) என்ன என்று கணக்கிடும்
 
Line 1,347 ⟶ 1,396:
 
பதிப்பி "நீங்கள் தந்த இரு எண்களின் மீபொம (மீச்சிறு பொது மடங்கு, LCM) = ", மீபொம(அ, ஆ)
</syntaxhighlight>
</lang>
 
=={{header|F_Sharp|F#}}==
<langsyntaxhighlight lang="fsharp">let rec gcd x y = if y = 0 then abs x else gcd y (x % y)
 
let lcm x y = x * y / (gcd x y)</langsyntaxhighlight>
 
=={{header|Factor}}==
The vocabulary ''math.functions'' already provides ''lcm''.
 
<langsyntaxhighlight lang="factor">USING: math.functions prettyprint ;
26 28 lcm .</langsyntaxhighlight>
 
This program outputs ''364''.
Line 1,364 ⟶ 1,413:
One can also reimplement ''lcm''.
 
<langsyntaxhighlight lang="factor">USING: kernel math prettyprint ;
IN: script
 
Line 1,375 ⟶ 1,424:
[ * abs ] [ gcd ] 2bi / ;
 
26 28 lcm .</langsyntaxhighlight>
 
=={{header|Fermat}}==
<langsyntaxhighlight lang="fermat">Func Lecm(a,b)=|a|*|b|/GCD(a,b).</langsyntaxhighlight>
 
=={{header|Forth}}==
<langsyntaxhighlight lang="forth">: gcd ( a b -- n )
begin dup while tuck mod repeat drop ;
 
: lcm ( a b -- n )
over 0= over 0= or if 2drop 0 exit then
2dup gcd abs */ ;</langsyntaxhighlight>
 
=={{header|Fortran}}==
This solution is written as a combination of 2 functions, but a subroutine implementation would work great as well.
<syntaxhighlight lang="fortran">
<lang Fortran>
integer function lcm(a,b)
integer:: a,b
Line 1,405 ⟶ 1,454:
gcd = abs(a)
end function gcd
</syntaxhighlight>
</lang>
 
=={{header|FreeBASIC}}==
===Iterative solution===
<langsyntaxhighlight lang="freebasic">' FB 1.05.0 Win64
 
Function lcm (m As Integer, n As Integer) As Integer
Line 1,426 ⟶ 1,475:
Print
Print "Press any key to quit"
Sleep</langsyntaxhighlight>
 
{{out}}
Line 1,437 ⟶ 1,486:
===Recursive solution===
Reuses code from [[Greatest_common_divisor#Recursive_solution]] and correctly handles negative arguments
<langsyntaxhighlight lang="freebasic">function gcdp( a as uinteger, b as uinteger ) as uinteger
if b = 0 then return a
return gcdp( b, a mod b )
Line 1,453 ⟶ 1,502:
print "lcm( 15, 12) = "; lcm(15, 12)
print "lcm(-10, -14) = "; lcm(-10, -14)
print "lcm( 0, 1) = "; lcm(0,1)</langsyntaxhighlight>
 
{{out}}
Line 1,465 ⟶ 1,514:
=={{header|Frink}}==
Frink has a built-in LCM function that handles arbitrarily-large integers.
<langsyntaxhighlight lang="frink">
println[lcm[2562047788015215500854906332309589561, 6795454494268282920431565661684282819]]
</syntaxhighlight>
</lang>
 
=={{header|FunL}}==
FunL has function <code>lcm</code> in module <code>integers</code> with the following definition:
 
<langsyntaxhighlight lang="funl">def
lcm( _, 0 ) = 0
lcm( 0, _ ) = 0
lcm( x, y ) = abs( (x\gcd(x, y)) y )</langsyntaxhighlight>
 
=={{header|GAP}}==
<langsyntaxhighlight lang="gap"># Built-in
LcmInt(12, 18);
# 36</langsyntaxhighlight>
 
=={{header|Go}}==
<langsyntaxhighlight lang="go">package main
 
import (
Line 1,499 ⟶ 1,548:
func main() {
fmt.Println(z.Mul(z.Div(&m, z.GCD(nil, nil, &m, &n)), &n))
}</langsyntaxhighlight>
{{out}}
<pre>
Line 1,506 ⟶ 1,555:
 
=={{header|Groovy}}==
<langsyntaxhighlight lang="groovy">def gcd
gcd = { m, n -> m = m.abs(); n = n.abs(); n == 0 ? m : m%n == 0 ? n : gcd(n, m % n) }
 
Line 1,516 ⟶ 1,565:
println "LCD of $t.m, $t.n is $t.l"
assert lcd(t.m, t.n) == t.l
}</langsyntaxhighlight>
{{out}}
<pre>LCD of 12, 18 is 36
Line 1,525 ⟶ 1,574:
{{trans|C}}
{{works with|PC-BASIC|any}}
<langsyntaxhighlight lang="qbasic">
10 PRINT "LCM(35, 21) = ";
20 LET MLCM = 35
Line 1,548 ⟶ 1,597:
450 LET GCD = NGCD
460 RETURN
</syntaxhighlight>
</lang>
 
=={{header|Haskell}}==
Line 1,554 ⟶ 1,603:
That is already available as the function ''lcm'' in the Prelude. Here's the implementation:
 
<langsyntaxhighlight lang="haskell">lcm :: (Integral a) => a -> a -> a
lcm _ 0 = 0
lcm 0 _ = 0
lcm x y = abs ((x `quot` (gcd x y)) * y)</langsyntaxhighlight>
 
=={{header|Icon}} and {{header|Unicon}}==
The lcm routine from the Icon Programming Library uses gcd. The routine is
 
<langsyntaxhighlight Iconlang="icon">link numbers
procedure main()
write("lcm of 18, 36 = ",lcm(18,36))
write("lcm of 0, 9 = ",lcm(0,9))
end</langsyntaxhighlight>
 
{{libheader|Icon Programming Library}}
[http://www.cs.arizona.edu/icon/library/src/procs/numbers.icn numbers provides lcm and gcd] and looks like this:
<langsyntaxhighlight Iconlang="icon">procedure lcm(i, j) #: least common multiple
if (i = 0) | (j = 0) then return 0
return abs(i * j) / gcd(i, j)
end</langsyntaxhighlight>
 
=={{header|J}}==
J provides the dyadic verb <code>*.</code> which returns the least common multiple of its left and right arguments.
 
<langsyntaxhighlight lang="j"> 12 *. 18
36
12 *. 18 22
Line 1,588 ⟶ 1,637:
*./~ 0 1
0 0
0 1</langsyntaxhighlight>
 
Note: least common multiple is the original boolean multiplication. Constraining the universe of values to 0 and 1 allows us to additionally define logical negation (and boolean algebra was redefined to include this constraint in the early 1900s - the original concept of boolean algebra is now known as a boolean ring (though, talking to some people: there's been some linguistic drift even there)).
 
=={{header|Java}}==
<langsyntaxhighlight lang="java">import java.util.Scanner;
 
public class LCM{
Line 1,619 ⟶ 1,668:
System.out.println("lcm(" + m + ", " + n + ") = " + lcm);
}
}</langsyntaxhighlight>
 
=={{header|JavaScript}}==
Line 1,630 ⟶ 1,679:
<math>\operatorname{lcm}(a_1,a_2,\ldots,a_n) = \operatorname{lcm}(\operatorname{lcm}(a_1,a_2,\ldots,a_{n-1}),a_n).</math>
 
<langsyntaxhighlight lang="javascript">function LCM(A) // A is an integer array (e.g. [-50,25,-45,-18,90,447])
{
var n = A.length, a = Math.abs(A[0]);
Line 1,643 ⟶ 1,692:
/* For example:
LCM([-50,25,-45,-18,90,447]) -> 67050
*/</langsyntaxhighlight>
 
 
===ES6===
{{Trans|Haskell}}
<langsyntaxhighlight JavaScriptlang="javascript">(() => {
'use strict';
 
Line 1,665 ⟶ 1,714:
return lcm(12, 18);
 
})();</langsyntaxhighlight>
 
{{Out}}
Line 1,672 ⟶ 1,721:
=={{header|jq}}==
Direct method
<langsyntaxhighlight lang="jq"># Define the helper function to take advantage of jq's tail-recursion optimization
def lcm(m; n):
def _lcm:
# state is [m, n, i]
if (.[2] % .[1]) == 0 then .[2] else (.[0:2] + [.[2] + m]) | _lcm end;
[m, n, m] | _lcm; </langsyntaxhighlight>
 
=={{header|Julia}}==
Built-in function:
<syntaxhighlight lang ="julia">lcm(m,n)</langsyntaxhighlight>
 
=={{header|K}}==
===K3===
<lang K> gcd:{:[~x;y;_f[y;x!y]]}
{{works with|Kona}}
<syntaxhighlight lang="k"> gcd:{:[~x;y;_f[y;x!y]]}
lcm:{_abs _ x*y%gcd[x;y]}
 
lcm .'(12 18; -6 14; 35 0)
36 42 0
lcm/1+!20
232792560</syntaxhighlight>
===K6===
{{works with|ngn/k}}
<syntaxhighlight lang="k"> abs:|/-:\
gcd:{$[~x;y;o[x!y;x]]}
lcm:{abs[`i$x*y%gcd[x;y]]}
 
lcm .'(12 18; -6 14; 35 0)
36 42 0
lcm/1+!20
232792560</langsyntaxhighlight>
 
=={{header|Klingphix}}==
<langsyntaxhighlight Klingphixlang="klingphix">:gcd { u v -- n }
abs int swap abs int swap
Line 1,709 ⟶ 1,769:
12 18 lcm print nl { 36 }
 
"End " input</langsyntaxhighlight>
 
=={{header|Kotlin}}==
<langsyntaxhighlight lang="scala">fun main(args: Array<String>) {
fun gcd(a: Long, b: Long): Long = if (b == 0L) a else gcd(b, a % b)
fun lcm(a: Long, b: Long): Long = a / gcd(a, b) * b
println(lcm(15, 9))
}
</syntaxhighlight>
</lang>
 
=={{header|LabVIEW}}==
Line 1,723 ⟶ 1,783:
 
=={{header|Lasso}}==
<langsyntaxhighlight Lassolang="lasso">define gcd(a,b) => {
while(#b != 0) => {
local(t = #b)
Line 1,741 ⟶ 1,801:
lcm(12, 18)
lcm(12, 22)
lcm(7, 31)</langsyntaxhighlight>
{{out}}
<pre>42
Line 1,750 ⟶ 1,810:
 
=={{header|Liberty BASIC}}==
<langsyntaxhighlight lang="lb">print "Least Common Multiple of 12 and 18 is "; LCM(12, 18)
end
 
Line 1,765 ⟶ 1,825:
GCD = abs(a)
end function
</langsyntaxhighlight>
 
=={{header|Logo}}==
<langsyntaxhighlight lang="logo">to abs :n
output sqrt product :n :n
end
Line 1,778 ⟶ 1,838:
to lcm :m :n
output quotient (abs product :m :n) gcd :m :n
end</langsyntaxhighlight>
 
Demo code:
 
<syntaxhighlight lang ="logo">print lcm 38 46</langsyntaxhighlight>
 
Output:
Line 1,789 ⟶ 1,849:
 
=={{header|Lua}}==
<langsyntaxhighlight lang="lua">function gcd( m, n )
while n ~= 0 do
local q = m
Line 1,802 ⟶ 1,862:
end
 
print( lcm(12,18) )</langsyntaxhighlight>
 
=={{header|m4}}==
 
This should work with any POSIX-compliant m4. I have tested it with OpenBSD m4, GNU m4, and Heirloom Devtools m4.
<langsyntaxhighlight lang="m4">divert(-1)
 
define(`gcd',
Line 1,826 ⟶ 1,886:
lcm(12, 18) = 36
lcm(12, 22) = 132
lcm(7, 31) = 217</langsyntaxhighlight>
 
{{out}}
Line 1,837 ⟶ 1,897:
=={{header|Maple}}==
The least common multiple of two integers is computed by the built-in procedure ilcm in Maple. This should not be confused with lcm, which computes the least common multiple of polynomials.
<langsyntaxhighlight Maplelang="maple">> ilcm( 12, 18 );
36
</syntaxhighlight>
</lang>
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
 
<langsyntaxhighlight Mathematicalang="mathematica">LCM[18,12]
-> 36</langsyntaxhighlight>
 
=={{header|MATLAB}} / {{header|Octave}}==
<syntaxhighlight lang Matlab="matlab"> lcm(a,b) </langsyntaxhighlight>
 
=={{header|Maxima}}==
<langsyntaxhighlight lang="maxima">lcm(a, b); /* a and b may be integers or polynomials */
 
/* In Maxima the gcd of two integers is always positive, and a * b = gcd(a, b) * lcm(a, b),
so the lcm may be negative. To get a positive lcm, simply do */
 
abs(lcm(a, b))</langsyntaxhighlight>
 
=={{header|Microsoft Small Basic}}==
{{trans|C}}
<langsyntaxhighlight lang="microsoftsmallbasic">
Textwindow.Write("LCM(35, 21) = ")
mlcm = 35
Line 1,881 ⟶ 1,941:
gcd = ngcd
EndSub
</syntaxhighlight>
</lang>
 
=={{header|MiniScript}}==
<langsyntaxhighlight MiniScriptlang="miniscript">gcd = function(a, b)
while b
temp = b
Line 1,899 ⟶ 1,959:
 
print lcm(18,12)
</syntaxhighlight>
</lang>
{{output}}
<pre>36</pre>
=={{header|MiniZinc}}==
<langsyntaxhighlight MiniZinclang="minizinc">function var int: lcm(int: a2,int:b2) =
let {
int:a1 = max(a2,b2);
Line 1,922 ⟶ 1,982:
var int: lcm1 = lcm(18,12);
solve satisfy;
output [show(lcm1),"\n"];</langsyntaxhighlight>
{{output}}
<pre>36</pre>
=={{header|min}}==
{{works with|min|0.19.6}}
<langsyntaxhighlight lang="min">((0 <) (-1 *) when) :abs
((dup 0 ==) (pop abs) (swap over mod) () linrec) :gcd
(over over gcd '* dip div) :lcm</langsyntaxhighlight>
 
=={{header|МК-61/52}}==
<syntaxhighlight lang="text">ИПA ИПB * |x| ПC ИПA ИПB / [x] П9
ИПA ИПB ПA ИП9 * - ПB x=0 05 ИПC
ИПA / С/П</langsyntaxhighlight>
 
=={{header|ML}}==
==={{header|mLite}}===
<langsyntaxhighlight lang="ocaml">fun gcd (a, 0) = a
| (0, b) = b
| (a, b) where (a < b)
Line 1,947 ⟶ 2,007:
in a * b div d
end
</syntaxhighlight>
</lang>
 
=={{header|Modula-2}}==
{{trans|C}}
{{works with|ADW Modula-2|any (Compile with the linker option ''Console Application'').}}
<langsyntaxhighlight lang="modula2">
MODULE LeastCommonMultiple;
 
Line 1,982 ⟶ 2,042:
WriteLn;
END LeastCommonMultiple.
</syntaxhighlight>
</lang>
 
=={{header|Nanoquery}}==
<langsyntaxhighlight Nanoquerylang="nanoquery">def gcd(a, b)
if (a < 1) or (b < 1)
throw new(InvalidNumberException, "gcd cannot be calculated on values < 1")
Line 2,006 ⟶ 2,066:
println lcm(12, 18)
println lcm(6, 14)
println lcm(1,2) = lcm(2,1)</langsyntaxhighlight>
 
{{out}}
Line 2,014 ⟶ 2,074:
 
=={{header|NetRexx}}==
<langsyntaxhighlight NetRexxlang="netrexx">/* NetRexx */
options replace format comments java crossref symbols nobinary
 
Line 2,084 ⟶ 2,144:
 
return
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 2,109 ⟶ 2,169:
=={{header|Nim}}==
The standard module "math" provides a function "lcm" for two integers and for an open array of integers. If we absolutely want to compute the least common multiple with our own procedure, it can be done this way (less efficient than the function in the standard library which avoids the modulo):
<langsyntaxhighlight lang="nim">proc gcd(u, v: int): auto =
var
u = u
Line 2,121 ⟶ 2,181:
 
echo lcm(12, 18)
echo lcm(-6, 14)</langsyntaxhighlight>
 
{{out}}
Line 2,129 ⟶ 2,189:
=={{header|Objeck}}==
{{trans|C}}
<langsyntaxhighlight lang="objeck">
class LCM {
function : Main(args : String[]) ~ Nil {
Line 2,145 ⟶ 2,205:
}
}
</syntaxhighlight>
</lang>
 
=={{header|OCaml}}==
 
<langsyntaxhighlight lang="ocaml">let rec gcd u v =
if v <> 0 then (gcd v (u mod v))
else (abs u)
Line 2,159 ⟶ 2,219:
 
let () =
Printf.printf "lcm(35, 21) = %d\n" (lcm 21 35)</langsyntaxhighlight>
 
=={{header|Oforth}}==
 
lcm is already defined into Integer class :
<syntaxhighlight lang Oforth="oforth">12 18 lcm</langsyntaxhighlight>
 
=={{header|ooRexx}}==
<syntaxhighlight lang="oorexx">
<lang ooRexx>
say lcm(18, 12)
 
Line 2,186 ⟶ 2,246:
use arg x, y
return x / gcd(x, y) * y
</syntaxhighlight>
</lang>
 
=={{header|Order}}==
{{trans|bc}}
<langsyntaxhighlight lang="c">#include <order/interpreter.h>
 
#define ORDER_PP_DEF_8gcd ORDER_PP_FN( \
Line 2,203 ⟶ 2,263:
// No support for negative numbers
 
ORDER_PP( 8to_lit(8lcm(12, 18)) ) // 36</langsyntaxhighlight>
 
=={{header|PARI/GP}}==
Built-in function:
<syntaxhighlight lang ="parigp">lcm</langsyntaxhighlight>
 
=={{header|Pascal}}==
<langsyntaxhighlight lang="pascal">Program LeastCommonMultiple(output);
 
{$IFDEF FPC}
Line 2,225 ⟶ 2,285:
begin
writeln('The least common multiple of 12 and 18 is: ', lcm(12, 18));
end.</langsyntaxhighlight>
Output:
<pre>The least common multiple of 12 and 18 is: 36
Line 2,232 ⟶ 2,292:
=={{header|Perl}}==
Using GCD:
<langsyntaxhighlight Perllang="perl">sub gcd {
my ($x, $y) = @_;
while ($x) { ($x, $y) = ($y % $x, $x) }
Line 2,243 ⟶ 2,303:
}
 
print lcm(1001, 221);</langsyntaxhighlight>
Or by repeatedly increasing the smaller of the two until LCM is reached:<langsyntaxhighlight lang="perl">sub lcm {
use integer;
my ($x, $y) = @_;
Line 2,256 ⟶ 2,316:
}
 
print lcm(1001, 221);</langsyntaxhighlight>
 
=={{header|Phix}}==
It is a builtin function, defined in builtins\gcd.e and accepting either two numbers or a single sequence of any length.
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #0000FF;">?</span><span style="color: #7060A8;">lcm</span><span style="color: #0000FF;">(</span><span style="color: #000000;">12</span><span style="color: #0000FF;">,</span><span style="color: #000000;">18</span><span style="color: #0000FF;">)</span>
<span style="color: #0000FF;">?</span><span style="color: #7060A8;">lcm</span><span style="color: #0000FF;">({</span><span style="color: #000000;">12</span><span style="color: #0000FF;">,</span><span style="color: #000000;">18</span><span style="color: #0000FF;">})</span>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 2,272 ⟶ 2,332:
 
=={{header|Phixmonti}}==
<langsyntaxhighlight Phixmontilang="phixmonti">def gcd /# u v -- n #/
abs int swap abs int swap
 
Line 2,286 ⟶ 2,346:
enddef
 
12345 50 lcm print</langsyntaxhighlight>
 
=={{header|PHP}}==
{{trans|D}}
<langsyntaxhighlight lang="php">echo lcm(12, 18) == 36;
 
function lcm($m, $n) {
Line 2,305 ⟶ 2,365:
}
return $a;
}</langsyntaxhighlight>
 
=={{header|Picat}}==
Line 2,311 ⟶ 2,371:
 
===Function===
<langsyntaxhighlight Picatlang="picat">lcm(X,Y)= abs(X*Y)//gcd(X,Y).</langsyntaxhighlight>
 
===Predicate===
<langsyntaxhighlight Picatlang="picat">lcm(X,Y,LCM) => LCM = abs(X*Y)//gcd(X,Y).</langsyntaxhighlight>
 
===Functional (fold/3)===
<langsyntaxhighlight Picatlang="picat">lcm(List) = fold(lcm,1,List).</langsyntaxhighlight>
 
===Test===
<langsyntaxhighlight Picatlang="picat">go =>
L = [
[12,18],
Line 2,335 ⟶ 2,395:
println('1..50'=lcm(1..50)),
nl.
</syntaxhighlight>
</lang>
 
{{out}}
Line 2,348 ⟶ 2,408:
=={{header|PicoLisp}}==
Using 'gcd' from [[Greatest common divisor#PicoLisp]]:
<langsyntaxhighlight PicoLisplang="picolisp">(de lcm (A B)
(abs (*/ A B (gcd A B))) )</langsyntaxhighlight>
 
=={{header|PL/I}}==
<syntaxhighlight lang="pl/i">
<lang PL/I>
/* Calculate the Least Common Multiple of two integers. */
 
Line 2,377 ⟶ 2,437:
end GCD;
end LCM;
</syntaxhighlight>
</lang>
<pre>
The LCM of 14 and 35 is 70
Line 2,384 ⟶ 2,444:
=={{header|PowerShell}}==
===version 1===
<syntaxhighlight lang="powershell">
<lang PowerShell>
function gcd ($a, $b) {
function pgcd ($n, $m) {
Line 2,401 ⟶ 2,461:
}
lcm 12 18
</syntaxhighlight>
</lang>
 
===version 2===
version2 is faster than version1
 
<syntaxhighlight lang="powershell">
<lang PowerShell>
function gcd ($a, $b) {
function pgcd ($n, $m) {
Line 2,423 ⟶ 2,483:
}
lcm 12 18
</syntaxhighlight>
</lang>
 
<b>Output:</b>
Line 2,432 ⟶ 2,492:
=={{header|Prolog}}==
SWI-Prolog knows gcd.
<langsyntaxhighlight Prologlang="prolog">lcm(X, Y, Z) :-
Z is abs(X * Y) / gcd(X,Y).</langsyntaxhighlight>
 
Example:
Line 2,441 ⟶ 2,501:
 
=={{header|PureBasic}}==
<langsyntaxhighlight PureBasiclang="purebasic">Procedure GCDiv(a, b); Euclidean algorithm
Protected r
While b
Line 2,457 ⟶ 2,517:
EndIf
ProcedureReturn t*Sign(t)
EndProcedure</langsyntaxhighlight>
 
=={{header|Python}}==
Line 2,463 ⟶ 2,523:
====gcd====
Using the fractions libraries [http://docs.python.org/library/fractions.html?highlight=fractions.gcd#fractions.gcd gcd] function:
<langsyntaxhighlight lang="python">>>> import fractions
>>> def lcm(a,b): return abs(a * b) / fractions.gcd(a,b) if a and b else 0
 
Line 2,471 ⟶ 2,531:
42
>>> assert lcm(0, 2) == lcm(2, 0) == 0
>>> </langsyntaxhighlight>
 
Or, for compositional flexibility, a curried '''lcm''', expressed in terms of our own '''gcd''' function:
<langsyntaxhighlight lang="python">'''Least common multiple'''
 
from inspect import signature
Line 2,571 ⟶ 2,631:
# MAIN ---
if __name__ == '__main__':
main()</langsyntaxhighlight>
{{Out}}
<pre>Least common multiples of 60 and [12..20]:
Line 2,593 ⟶ 2,653:
====Prime decomposition====
This imports [[Prime decomposition#Python]]
<langsyntaxhighlight lang="python">from prime_decomposition import decompose
try:
reduce
Line 2,614 ⟶ 2,674:
print( lcm(12, 18) ) # 36
print( lcm(-6, 14) ) # 42
assert lcm(0, 2) == lcm(2, 0) == 0</langsyntaxhighlight>
 
====Iteration over multiples====
<langsyntaxhighlight lang="python">>>> def lcm(*values):
values = set([abs(int(v)) for v in values])
if values and 0 not in values:
Line 2,635 ⟶ 2,695:
>>> lcm(12, 18, 22)
396
>>> </langsyntaxhighlight>
 
====Repeated modulo====
{{trans|Tcl}}
<langsyntaxhighlight lang="python">>>> def lcm(p,q):
p, q = abs(p), abs(q)
m = p * q
Line 2,656 ⟶ 2,716:
>>> lcm(2, 0)
0
>>> </langsyntaxhighlight>
 
=={{header|Qi}}==
<syntaxhighlight lang="qi">
<lang qi>
(define gcd
A 0 -> A
Line 2,665 ⟶ 2,725:
 
(define lcm A B -> (/ (* A B) (gcd A B)))
</syntaxhighlight>
</lang>
 
=={{header|Quackery}}==
 
<langsyntaxhighlight Quackerylang="quackery">[ [ dup while
tuck mod again ]
drop abs ] is gcd ( n n --> n )
Line 2,676 ⟶ 2,736:
[ 2dup gcd
/ * abs ]
else and ] is lcm ( n n --> n )</langsyntaxhighlight>
 
=={{header|R}}==
<syntaxhighlight lang="r">
<lang R>
"%gcd%" <- function(u, v) {ifelse(u %% v != 0, v %gcd% (u%%v), v)}
 
Line 2,685 ⟶ 2,745:
 
print (50 %lcm% 75)
</syntaxhighlight>
</lang>
 
=={{header|Racket}}==
Racket already has defined both lcm and gcd funtions:
<langsyntaxhighlight Racketlang="racket">#lang racket
(lcm 3 4 5 6) ;returns 60
(lcm 8 108) ;returns 216
(gcd 8 108) ;returns 4
(gcd 108 216 432) ;returns 108</langsyntaxhighlight>
 
=={{header|Raku}}==
(formerly Perl 6)
This function is provided as an infix so that it can be used productively with various metaoperators.
<syntaxhighlight lang="raku" perl6line>say 3 lcm 4; # infix
say [lcm] 1..20; # reduction
say ~(1..10 Xlcm 1..10) # cross</langsyntaxhighlight>
{{out}}
<pre>12
Line 2,709 ⟶ 2,769:
This is from the math extensions library included with Retro.
 
<langsyntaxhighlight Retrolang="retro">: gcd ( ab-n ) [ tuck mod dup ] while drop ;
: lcm ( ab-n ) 2over gcd [ * ] dip / ;</langsyntaxhighlight>
 
=={{header|REXX}}==
Line 2,719 ⟶ 2,779:
 
Usage note: &nbsp; the integers can be expressed as a list and/or specified as individual arguments &nbsp; (or as mixed).
<langsyntaxhighlight lang="rexx">/*REXX program finds the LCM (Least Common Multiple) of any number of integers. */
numeric digits 10000 /*can handle 10k decimal digit numbers.*/
say 'the LCM of 19 and 0 is ───► ' lcm(19 0 )
Line 2,742 ⟶ 2,802:
x=d%x /*divide the pre─calculated value.*/
end /*while*/ /* [↑] process subsequent args. */
return x /*return with the LCM of the args.*/</langsyntaxhighlight>
'''output''' &nbsp; when using the (internal) supplied list:
<pre>
Line 2,757 ⟶ 2,817:
{{trans|REXX version 0}} using different argument handling-
Use as lcm(a,b,c,---)
<langsyntaxhighlight lang="rexx">lcm2: procedure
x=abs(arg(1))
do k=2 to arg() While x<>0
Line 2,777 ⟶ 2,837:
end
end
return x</langsyntaxhighlight>
 
=={{header|Ring}}==
<syntaxhighlight lang="text">
see lcm(24,36)
Line 2,794 ⟶ 2,854:
end
return gcd
</syntaxhighlight>
</lang>
 
=={{header|RPL}}==
'''For unsigned integers'''
≪ DUP2 < ≪ SWAP ≫ IFT
'''WHILE''' DUP B→R '''REPEAT'''
SWAP OVER / LAST ROT * - '''END''' DROP
≫ '<span style="color:blue">GCD</span>' STO
≪ DUP2 * ROT ROT <span style="color:blue">GCD</span> /
≫ '<span style="color:blue">LCM</span>' STO
#12d #18d <span style="color:blue">LCM</span>
{{out}}
<pre>
1: #36d
</pre>
'''For usual integers''' (floating point without decimal part)
≪ '''WHILE''' DUP '''REPEAT'''
SWAP OVER MOD '''END''' DROP ABS
≫ '<span style="color:blue">GCD</span>' STO
≪ DUP2 * ROT ROT <span style="color:blue">GCD</span> /
≫ '<span style="color:blue">LCM</span>' STO
 
=={{header|Ruby}}==
Ruby has an <tt>Integer#lcm</tt> method, which finds the least common multiple of two integers.
 
<langsyntaxhighlight lang="ruby">irb(main):001:0> 12.lcm 18
=> 36</langsyntaxhighlight>
 
I can also write my own <tt>lcm</tt> method. This one takes any number of arguments.
 
<langsyntaxhighlight lang="ruby">def gcd(m, n)
m, n = n, m % n until n.zero?
m.abs
Line 2,817 ⟶ 2,900:
 
p lcm 12, 18, 22
p lcm 15, 14, -6, 10, 21</langsyntaxhighlight>
 
{{out}}
Line 2,828 ⟶ 2,911:
{{works with|Just BASIC}}
{{works with|Liberty BASIC}}
<langsyntaxhighlight lang="lb">print "lcm( 12, -18) = "; lcm( 12, -18)
print "lcm( 15, 12) = "; lcm( 15, 12)
print "lcm(-10, -14) = "; lcm(-10, -14)
Line 2,845 ⟶ 2,928:
wend
GCD = abs(a)
end function</langsyntaxhighlight>
 
=={{header|Rust}}==
This implementation uses a recursive implementation of Stein's algorithm to calculate the gcd.
<langsyntaxhighlight lang="rust">use std::cmp::{max, min};
 
fn gcd(a: usize, b: usize) -> usize {
Line 2,871 ⟶ 2,954:
fn main() {
println!("{}", lcm(6324, 234))
}</langsyntaxhighlight>
 
=={{header|Scala}}==
<langsyntaxhighlight lang="scala">def gcd(a: Int, b: Int):Int=if (b==0) a.abs else gcd(b, a%b)
def lcm(a: Int, b: Int)=(a*b).abs/gcd(a,b)</langsyntaxhighlight>
<langsyntaxhighlight lang="scala">lcm(12, 18) // 36
lcm( 2, 0) // 0
lcm(-6, 14) // 42</langsyntaxhighlight>
 
=={{header|Scheme}}==
<langsyntaxhighlight lang="scheme">
>(define gcd (lambda (a b)
(if (zero? b)
Line 2,891 ⟶ 2,974:
(abs (* b (floor (/ a (gcd a b))))))))
>(lcm 12 18)
36</langsyntaxhighlight>
 
=={{header|Seed7}}==
<langsyntaxhighlight lang="seed7">$ include "seed7_05.s7i";
 
const func integer: gcd (in var integer: a, in var integer: b) is func
Line 2,916 ⟶ 2,999:
begin
writeln("lcm(35, 21) = " <& lcm(21, 35));
end func;</langsyntaxhighlight>
 
Original source: [http://seed7.sourceforge.net/algorith/math.htm#lcm]
 
=={{header|SenseTalk}}==
<langsyntaxhighlight lang="sensetalk">function gcd m, n
repeat while m is greater than 0
put m into temp
Line 2,932 ⟶ 3,015:
function lcm m, n
return m divided by gcd(m, n) times n
end lcm</langsyntaxhighlight>
 
=={{header|Sidef}}==
Built-in:
<langsyntaxhighlight lang="ruby">say Math.lcm(1001, 221)</langsyntaxhighlight>
 
Using GCD:
<langsyntaxhighlight lang="ruby">func gcd(a, b) {
while (a) { (a, b) = (b % a, a) }
return b
Line 2,948 ⟶ 3,031:
}
 
say lcm(1001, 221)</langsyntaxhighlight>
{{out}}
<pre>
Line 2,956 ⟶ 3,039:
=={{header|Smalltalk}}==
Smalltalk has a built-in <code>lcm</code> method on <code>SmallInteger</code>:
<syntaxhighlight lang ="smalltalk">12 lcm: 18</langsyntaxhighlight>
 
=={{header|Sparkling}}==
<langsyntaxhighlight lang="sparkling">function factors(n) {
var f = {};
 
Line 2,991 ⟶ 3,074:
function LCM(n, k) {
return n * k / GCD(n, k);
}</langsyntaxhighlight>
 
=={{header|Standard ML}}==
===Readable version===
<lang sml>val rec gcd = fn (x, 0) => abs x | p as (_, y) => gcd (y, Int.rem p)
<syntaxhighlight lang="sml">fun gcd (0,n) = n
| gcd (m,n) = gcd(n mod m, m)
 
fun lcm (m,n) = abs(x * y) div gcd (m, n)</syntaxhighlight>
 
===Alternate version===
<syntaxhighlight lang="sml">val rec gcd = fn (x, 0) => abs x | p as (_, y) => gcd (y, Int.rem p)
 
val lcm = fn p as (x, y) => Int.quot (abs (x * y), gcd p)</langsyntaxhighlight>
 
=={{header|Swift}}==
Using the Swift GCD function.
<langsyntaxhighlight Swiftlang="swift">func lcm(a:Int, b:Int) -> Int {
return abs(a * b) / gcd_rec(a, b)
}</langsyntaxhighlight>
 
=={{header|Tcl}}==
<langsyntaxhighlight lang="tcl">proc lcm {p q} {
set m [expr {$p * $q}]
if {!$m} {return 0}
Line 3,014 ⟶ 3,104:
if {!$q} {return [expr {$m / $p}]}
}
}</langsyntaxhighlight>
Demonstration
<syntaxhighlight lang ="tcl">puts [lcm 12 18]</langsyntaxhighlight>
Output:
36
 
=={{header|TI-83 BASIC}}==
<langsyntaxhighlight lang="ti83b">lcm(12,18
36</langsyntaxhighlight>
 
=={{header|TSE SAL}}==
<langsyntaxhighlight TSESALlang="tsesal">// library: math: get: least: common: multiple <description></description> <version control></version control> <version>1.0.0.0.2</version> <version control></version control> (filenamemacro=getmacmu.s) [<Program>] [<Research>] [kn, ri, su, 20-01-2013 14:36:11]
INTEGER PROC FNMathGetLeastCommonMultipleI( INTEGER x1I, INTEGER x2I )
//
Line 3,054 ⟶ 3,144:
Warn( FNMathGetLeastCommonMultipleI( Val( s1 ), Val( s2 ) ) ) // gives e.g. 10
UNTIL FALSE
END</langsyntaxhighlight>
 
=={{header|TXR}}==
 
<langsyntaxhighlight lang="bash">$ txr -p '(lcm (expt 2 123) (expt 6 49) 17)'
43259338018880832376582582128138484281161556655442781051813888</langsyntaxhighlight>
 
== {{header|TypeScript}} ==
{{trans|C}}
<langsyntaxhighlight lang="javascript">// Least common multiple
 
function gcd(m: number, n: number): number {
Line 3,080 ⟶ 3,170:
 
console.log(`LCM(35, 21) = ${lcm(35, 21)}`);
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 3,088 ⟶ 3,178:
=={{header|uBasic/4tH}}==
{{trans|BBC BASIC}}
<syntaxhighlight lang="text">Print "LCM of 12 : 18 = "; FUNC(_LCM(12,18))
 
End
Line 3,108 ⟶ 3,198:
Else
Return (0)
EndIf</langsyntaxhighlight>
{{out}}
<pre>LCM of 12 : 18 = 36
Line 3,118 ⟶ 3,208:
 
{{works with|Bourne Shell}}
<langsyntaxhighlight lang="bash">gcd() {
# Calculate $1 % $2 until $2 becomes zero.
until test 0 -eq "$2"; do
Line 3,138 ⟶ 3,228:
 
lcm 30 -42
# => 210</langsyntaxhighlight>
 
==={{header|C Shell}}===
<langsyntaxhighlight lang="csh">alias gcd eval \''set gcd_args=( \!*:q ) \\
@ gcd_u=$gcd_args[2] \\
@ gcd_v=$gcd_args[3] \\
Line 3,164 ⟶ 3,254:
lcm result 30 -42
echo $result
# => 210</langsyntaxhighlight>
 
=={{header|Ursa}}==
<langsyntaxhighlight lang="ursa">import "math"
out (lcm 12 18) endl console</langsyntaxhighlight>
{{out}}
<pre>36</pre>
 
=={{header|Vala}}==
<langsyntaxhighlight lang="vala">
int lcm(int a, int b){
/*Return least common multiple of two ints*/
Line 3,200 ⟶ 3,290:
stdout.printf("lcm(%d, %d) = %d\n", a, b, lcm(a, b));
}
</syntaxhighlight>
</lang>
 
=={{header|VBA}}==
<langsyntaxhighlight lang="vb">Function gcd(u As Long, v As Long) As Long
Dim t As Long
Do While v
Line 3,214 ⟶ 3,304:
Function lcm(m As Long, n As Long) As Long
lcm = Abs(m * n) / gcd(m, n)
End Function</langsyntaxhighlight>
 
=={{header|VBScript}}==
<langsyntaxhighlight lang="vb">Function LCM(a,b)
LCM = POS((a * b)/GCD(a,b))
End Function
Line 3,246 ⟶ 3,336:
 
WScript.StdOut.Write "The LCM of " & i & " and " & j & " is " & LCM(i,j) & "."
WScript.StdOut.WriteLine</langsyntaxhighlight>
 
{{out}}
Line 3,263 ⟶ 3,353:
=={{header|Wortel}}==
Operator
<syntaxhighlight lang ="wortel">@lcm a b</langsyntaxhighlight>
Number expression
<syntaxhighlight lang ="wortel">!#~km a b</langsyntaxhighlight>
Function (using gcd)
<langsyntaxhighlight lang="wortel">&[a b] *b /a @gcd a b</langsyntaxhighlight>
 
=={{header|Wren}}==
<langsyntaxhighlight ecmascriptlang="wren">var gcd = Fn.new { |x, y|
while (y != 0) {
var t = y
Line 3,287 ⟶ 3,377:
for (xy in xys) {
System.print("lcm(%(xy[0]), %(xy[1]))\t%("\b"*5) = %(lcm.call(xy[0], xy[1]))")
}</langsyntaxhighlight>
 
{{out}}
Line 3,299 ⟶ 3,389:
{{trans|C}}
{{works with|Windows XBasic}}
<langsyntaxhighlight lang="xbasic">' Least common multiple
PROGRAM "leastcommonmultiple"
VERSION "0.0001"
Line 3,325 ⟶ 3,415:
 
END PROGRAM
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 3,332 ⟶ 3,422:
 
=={{header|XPL0}}==
<langsyntaxhighlight XPL0lang="xpl0">include c:\cxpl\codes;
 
func GCD(M,N); \Return the greatest common divisor of M and N
Line 3,347 ⟶ 3,437:
 
\Display the LCM of two integers entered on command line
IntOut(0, LCM(IntIn(8), IntIn(8)))</langsyntaxhighlight>
 
=={{header|Yabasic}}==
<langsyntaxhighlight Yabasiclang="yabasic">sub gcd(u, v)
local t
Line 3,367 ⟶ 3,457:
end sub
 
print "Least common multiple: ", lcm(12345, 23044)</langsyntaxhighlight>
 
=={{header|zkl}}==
<langsyntaxhighlight lang="zkl">fcn lcm(m,n){ (m*n).abs()/m.gcd(n) } // gcd is a number method</langsyntaxhighlight>
{{out}}
<pre>
23

edits