Initial data commit

This commit is contained in:
Ingy döt Net 2023-07-01 11:58:00 -04:00
parent 72d218235f
commit f23f22d71c
199087 changed files with 3378941 additions and 0 deletions

View file

@ -0,0 +1,4 @@
---
category:
- Recursion
from: http://rosettacode.org/wiki/Least_common_multiple

View file

@ -0,0 +1,36 @@
;Task:
Compute the   least common multiple   (LCM)   of two integers.
Given   ''m''   and   ''n'',   the least common multiple is the smallest positive integer that has both   ''m''   and   ''n''   as factors.
;Example:
The least common multiple of   '''12'''   and   '''18'''   is   '''36''',       because:
:*   '''12'''   is a factor     ('''12''' × '''3''' = '''36'''),     and
:*   '''18'''   is a factor     ('''18''' × '''2''' = '''36'''),     and
:*   there is no positive integer less than   '''36'''   that has both factors.
As a special case,   if either   ''m''   or   ''n''   is zero,   then the least common multiple is zero.
One way to calculate the least common multiple is to iterate all the multiples of   ''m'',   until you find one that is also a multiple of   ''n''.
If you already have   ''gcd''   for [[greatest common divisor]],   then this formula calculates   ''lcm''.
<big>
:::: <math>\operatorname{lcm}(m, n) = \frac{|m \times n|}{\operatorname{gcd}(m, n)}</math>
</big>
One can also find &nbsp; ''lcm'' &nbsp; by merging the [[prime decomposition]]s of both &nbsp; ''m'' &nbsp; and &nbsp; ''n''.
;Related task
:* &nbsp; [https://rosettacode.org/wiki/Greatest_common_divisor greatest common divisor].
;See also:
* &nbsp; MathWorld entry: &nbsp; [http://mathworld.wolfram.com/LeastCommonMultiple.html Least Common Multiple].
* &nbsp; Wikipedia entry: &nbsp; [[wp:Least common multiple|Least common multiple]].
<br><br>

View file

@ -0,0 +1,9 @@
F gcd(=a, =b)
L b != 0
(a, b) = (b, a % b)
R a
F lcm(m, n)
R m I/ gcd(m, n) * n
print(lcm(12, 18))

View file

@ -0,0 +1,30 @@
LCM CSECT
USING LCM,R15 use calling register
L R6,A a
L R7,B b
LR R8,R6 c=a
LOOPW LR R4,R8 c
SRDA R4,32 shift to next reg
DR R4,R7 c/b
LTR R4,R4 while c mod b<>0
BZ ELOOPW leave while
AR R8,R6 c+=a
B LOOPW end while
ELOOPW LPR R9,R6 c=abs(u)
L R1,A a
XDECO R1,XDEC edit a
MVC PG+4(5),XDEC+7 move a to buffer
L R1,B b
XDECO R1,XDEC edit b
MVC PG+10(5),XDEC+7 move b to buffer
XDECO R8,XDEC edit c
MVC PG+17(10),XDEC+2 move c to buffer
XPRNT PG,80 print buffer
XR R15,R15 return code =0
BR R14 return to caller
A DC F'1764' a
B DC F'3920' b
PG DC CL80'lcm(00000,00000)=0000000000' buffer
XDEC DS CL12 temp for edit
YREGS
END LCM

View file

@ -0,0 +1,25 @@
: gcd \ a b -- gcd
dup 0 n:= if drop ;; then
tuck \ b a b
n:mod \ b a-mod-b
recurse ;
: lcm \ m n
2dup \ m n m n
n:* \ m n m*n
n:abs \ m n abs(m*n)
-rot \ abs(m*n) m n
gcd \ abs(m*n) gcd(m.n)
n:/mod \ abs / gcd
nip \ abs div gcd
;
: demo \ n m --
2dup "LCM of " . . " and " . . " = " . lcm . ;
12 18 demo cr
-6 14 demo cr
35 0 demo cr
bye

View file

@ -0,0 +1,15 @@
BEGIN
PROC gcd = (INT m, n) INT :
BEGIN
INT a := ABS m, b := ABS n;
IF a=0 OR b=0 THEN 0 ELSE
WHILE b /= 0 DO INT t = b; b := a MOD b; a := t OD;
a
FI
END;
PROC lcm = (INT m, n) INT : ( m*n = 0 | 0 | ABS (m*n) % gcd (m, n));
INT m=12, n=18;
printf (($gxg(0)3(xgxg(0))l$,
"The least common multiple of", m, "and", n, "is", lcm(m,n),
"and their greatest common divisor is", gcd(m,n)))
END

View file

@ -0,0 +1,9 @@
begin
integer procedure gcd ( integer value a, b ) ;
if b = 0 then a else gcd( b, a rem abs(b) );
integer procedure lcm( integer value a, b ) ;
abs( a * b ) div gcd( a, b );
write( lcm( 15, 20 ) );
end.

View file

@ -0,0 +1,2 @@
12^18
36

View file

@ -0,0 +1,3 @@
LCM{(|×)÷}
12 LCM 18
36

View file

@ -0,0 +1,102 @@
#define ATS_DYNLOADFLAG 0 (* No initialization is needed. *)
#include "share/atspre_define.hats"
#include "share/atspre_staload.hats"
(********************************************************************)
(* *)
(* Declarations. *)
(* *)
(* (These could be ported to a .sats file.) *)
(* *)
(* lcm for unsigned integer types without constraints. *)
extern fun {tk : tkind}
g0uint_lcm (u : g0uint tk,
v : g0uint tk) :<>
g0uint tk
(* The gcd template function to be expanded when g0uint_lcm is
expanded. Set it to your favorite gcd function. *)
extern fun {tk : tkind}
g0uint_lcm$gcd (u : g0uint tk,
v : g0uint tk) :<>
g0uint tk
(* lcm for signed integer types, giving unsigned results. *)
extern fun {tk_signed, tk_unsigned : tkind}
g0int_lcm (u : g0int tk_signed,
v : g0int tk_signed) :<>
g0uint tk_unsigned
overload lcm with g0uint_lcm
overload lcm with g0int_lcm
(********************************************************************)
(* *)
(* The implementations. *)
(* *)
implement {tk}
g0uint_lcm (u, v) =
let
val d = g0uint_lcm$gcd<tk> (u, v)
in
(* There is no need to take the absolute value, because this
implementation is strictly for unsigned integers. *)
(u * v) / d
end
implement {tk_signed, tk_unsigned}
g0int_lcm (u, v) =
let
extern castfn
unsigned :
g0int tk_signed -<> g0uint tk_unsigned
in
g0uint_lcm (unsigned (abs u), unsigned (abs v))
end
(********************************************************************)
(* *)
(* A test that it actually works. *)
(* *)
implement
main0 () =
let
implement {tk}
g0uint_lcm$gcd (u, v) =
(* An ugly gcd for the sake of demonstrating that it can be done
this way: Euclids algorithm written an the Algol style,
which is not a natural style in ATS. Almost always you want
to write a tail-recursive function, instead. I did, however
find the Algol style very useful when I was migrating
matrix routines from Fortran.
In reality, you would implement g0uint_lcm$gcd by having it
simply call whatever gcd template function you are using in
your program. *)
$effmask_all
begin
let
var x : g0uint tk = u
var y : g0uint tk = v
in
while (y <> 0)
let
val z = y
in
y := x mod z;
x := z
end;
x
end
end
in
assertloc (lcm (~6, 14) = 42U);
assertloc (lcm (2L, 0L) = 0ULL);
assertloc (lcm (12UL, 18UL) = 36UL);
assertloc (lcm (12, 22) = 132ULL);
assertloc (lcm (7ULL, 31ULL) = 217ULL)
end

View file

@ -0,0 +1,22 @@
# greatest common divisor
function gcd(m, n, t) {
# Euclid's method
while (n != 0) {
t = m
m = n
n = t % n
}
return m
}
# least common multiple
function lcm(m, n, r) {
if (m == 0 || n == 0)
return 0
r = m * n / gcd(m, n)
return r < 0 ? -r : r
}
# Read two integers from each line of input.
# Print their least common multiple.
{ print lcm($1, $2) }

View file

@ -0,0 +1,32 @@
CARD FUNC Lcm(CARD a,b)
CARD tmp,c
IF a=0 OR b=0 THEN
RETURN (0)
FI
IF a<b THEN
tmp=a a=b b=tmp
FI
c=0
DO
c==+1
UNTIL a*c MOD b=0
OD
RETURN(a*c)
PROC Test(CARD a,b)
CARD res
res=Lcm(a,b)
PrintF("LCM of %I and %I is %I%E",a,b,res)
RETURN
PROC Main()
Test(4,6)
Test(120,77)
Test(24,8)
Test(1,56)
Test(12,0)
RETURN

View file

@ -0,0 +1,28 @@
with Ada.Text_IO; use Ada.Text_IO;
procedure Lcm_Test is
function Gcd (A, B : Integer) return Integer is
M : Integer := A;
N : Integer := B;
T : Integer;
begin
while N /= 0 loop
T := M;
M := N;
N := T mod N;
end loop;
return M;
end Gcd;
function Lcm (A, B : Integer) return Integer is
begin
if A = 0 or B = 0 then
return 0;
end if;
return abs (A) * (abs (B) / Gcd (A, B));
end Lcm;
begin
Put_Line ("LCM of 12, 18 is" & Integer'Image (Lcm (12, 18)));
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;

View file

@ -0,0 +1,47 @@
------------------ LEAST COMMON MULTIPLE -----------------
-- lcm :: Integral a => a -> a -> a
on lcm(x, y)
if 0 = x or 0 = y then
0
else
abs(x div (gcd(x, y)) * y)
end if
end lcm
--------------------------- TEST -------------------------
on run
lcm(12, 18)
--> 36
end run
-------------------- GENERIC FUNCTIONS -------------------
-- abs :: Num a => a -> a
on abs(x)
if 0 > x then
-x
else
x
end if
end abs
-- gcd :: Integral a => a -> a -> a
on gcd(x, y)
script
on |λ|(a, b)
if 0 = b then
a
else
|λ|(b, a mod b)
end if
end |λ|
end script
result's |λ|(abs(x), abs(y))
end gcd

View file

@ -0,0 +1,22 @@
10 DEF FN MOD(A) = INT((A / B - INT(A / B)) * B + .05) * SGN(A / B)
20 INPUT"M=";M%
30 INPUT"N=";N%
40 GOSUB 100
50 PRINT R
60 END
100 REM LEAST COMMON MULTIPLE M% N%
110 R = 0
120 IF M% = 0 OR N% = 0 THEN RETURN
130 A% = M% : B% = N% : GOSUB 200"GCD
140 R = ABS(M%*N%)/R
150 RETURN
200 REM GCD ITERATIVE EUCLID A% B%
210 FOR B = B% TO 0 STEP 0
220 C% = A%
230 A% = B
240 B = FN MOD(C%)
250 NEXT B
260 R = ABS(A%)
270 RETURN

View file

@ -0,0 +1,5 @@
lcm: function [x,y][
x * y / gcd @[x y]
]
print lcm 12 18

View file

@ -0,0 +1,13 @@
LCM(Number1,Number2)
{
If (Number1 = 0 || Number2 = 0)
Return
Var := Number1 * Number2
While, Number2
Num := Number2, Number2 := Mod(Number1,Number2), Number1 := Num
Return, Var // Number1
}
Num1 = 12
Num2 = 18
MsgBox % LCM(Num1,Num2)

View file

@ -0,0 +1,13 @@
Func _LCM($a, $b)
Local $c, $f, $m = $a, $n = $b
$c = 1
While $c <> 0
$f = Int($a / $b)
$c = $a - $b * $f
If $c <> 0 Then
$a = $b
$b = $c
EndIf
WEnd
Return $m * $n / $b
EndFunc ;==>_LCM

View file

@ -0,0 +1,3 @@
ConsoleWrite(_LCM(12,18) & @LF)
ConsoleWrite(_LCM(-5,12) & @LF)
ConsoleWrite(_LCM(13,0) & @LF)

View file

@ -0,0 +1,16 @@
function mcm (m, n)
if m = 0 or n = 0 then return 0
if m < n then
t = m : m = n : n = t
end if
cont = 0
do
cont += 1
until (m * cont) mod n = 0
return m * cont
end function
print "lcm( 12, 18) = "; mcm( 12, -18)
print "lcm( 15, 12) = "; mcm( 15, 12)
print "lcm(-10, -14) = "; mcm(-10, -14)
print "lcm( 0, 1) = "; mcm( 0, 1)

View file

@ -0,0 +1,17 @@
function gcdp(a, b)
if b = 0 then return a
return gcdp(b, a mod b)
end function
function gcd(a, b)
return gcdp(abs(a), abs(b))
end function
function lcm(a, b)
return abs(a * b) / gcd(a, b)
end function
print "lcm( 12, -18) = "; lcm( 12, -18)
print "lcm( 15, 12) = "; lcm( 15, 12)
print "lcm(-10, -14) = "; lcm(-10, -14)
print "lcm( 0, 1) = "; lcm( 0, 1)

View file

@ -0,0 +1,11 @@
DEF FN_LCM(M%,N%)
IF M%=0 OR N%=0 THEN =0 ELSE =ABS(M%*N%)/FN_GCD_Iterative_Euclid(M%, N%)
DEF FN_GCD_Iterative_Euclid(A%, B%)
LOCAL C%
WHILE B%
C% = A%
A% = B%
B% = C% MOD B%
ENDWHILE
= ABS(A%)

View file

@ -0,0 +1,11 @@
get "libhdr"
let lcm(m,n) =
m=0 -> 0,
n=0 -> 0,
abs(m*n) / gcd(m,n)
and gcd(m,n) =
n=0 -> m,
gcd(n, m rem n)
let start() be writef("%N*N", lcm(12, 18))

View file

@ -0,0 +1 @@
Lcm ×÷{𝕨(|𝕊(>0))𝕩}

View file

@ -0,0 +1 @@
12 Lcm 18

View file

@ -0,0 +1,18 @@
@echo off
setlocal enabledelayedexpansion
set num1=12
set num2=18
call :lcm %num1% %num2%
exit /b
:lcm <input1> <input2>
if %2 equ 0 (
set /a lcm = %num1%*%num2%/%1
echo LCM = !lcm!
pause>nul
goto :EOF
)
set /a res = %1 %% %2
call :lcm %2 %res%
goto :EOF

View file

@ -0,0 +1,22 @@
/* greatest common divisor */
define g(m, n) {
auto t
/* Euclid's method */
while (n != 0) {
t = m
m = n
n = t % n
}
return (m)
}
/* least common multiple */
define l(m, n) {
auto r
if (m == 0 || n == 0) return (0)
r = m * n / g(m, n)
if (r < 0) return (-r)
return (r)
}

View file

@ -0,0 +1,4 @@
&>:0`2*1-*:&>:#@!#._:0`2*1v
>28*:*:**+:28*>:*:*/\:vv*-<
|<:%/*:*:*82\%*:*:*82<<>28v
>$/28*:*:*/*.@^82::+**:*:*<

View file

@ -0,0 +1,8 @@
(gcd=
a b
. !arg:(?a.?b)
& den$(!a*!b^-1)
* (!a:<0&-1|1)
* !a
);
out$(gcd$(12.18) gcd$(-6.14) gcd$(35.0) gcd$(117.18))

View file

@ -0,0 +1,12 @@
gcd = { a, b |
true? { a == 0 }
{ b }
{ gcd(b % a, a) }
}
lcm = { a, b |
a * b / gcd(a, b)
}
p lcm(12, 18) # 36
p lcm(14, 21) # 42

View file

@ -0,0 +1,9 @@
#include <boost/math/common_factor.hpp>
#include <iostream>
int main( ) {
std::cout << "The least common multiple of 12 and 18 is " <<
boost::math::lcm( 12 , 18 ) << " ,\n"
<< "and the greatest common divisor " << boost::math::gcd( 12 , 18 ) << " !" << std::endl ;
return 0 ;
}

View file

@ -0,0 +1,24 @@
#include <cstdlib>
#include <iostream>
#include <tuple>
int gcd(int a, int b) {
a = abs(a);
b = abs(b);
while (b != 0) {
std::tie(a, b) = std::make_tuple(b, a % b);
}
return a;
}
int lcm(int a, int b) {
int c = gcd(a, b);
return c == 0 ? 0 : a / c * b;
}
int main() {
std::cout << "The least common multiple of 12 and 18 is " << lcm(12, 18) << ",\n"
<< "and their greatest common divisor is " << gcd(12, 18) << "!"
<< std::endl;
return 0;
}

View file

@ -0,0 +1,24 @@
alias gcd eval \''set gcd_args=( \!*:q ) \\
@ gcd_u=$gcd_args[2] \\
@ gcd_v=$gcd_args[3] \\
while ( $gcd_v != 0 ) \\
@ gcd_t = $gcd_u % $gcd_v \\
@ gcd_u = $gcd_v \\
@ gcd_v = $gcd_t \\
end \\
if ( $gcd_u < 0 ) @ gcd_u = - $gcd_u \\
@ $gcd_args[1]=$gcd_u \\
'\'
alias lcm eval \''set lcm_args=( \!*:q ) \\
@ lcm_m = $lcm_args[2] \\
@ lcm_n = $lcm_args[3] \\
gcd lcm_d $lcm_m $lcm_n \\
@ lcm_r = ( $lcm_m * $lcm_n ) / $lcm_d \\
if ( $lcm_r < 0 ) @ lcm_r = - $lcm_r \\
@ $lcm_args[1] = $lcm_r \\
'\'
lcm result 30 -42
echo $result
# => 210

View file

@ -0,0 +1,16 @@
Using System;
class Program
{
static int gcd(int m, int n)
{
return n == 0 ? Math.Abs(m) : gcd(n, n % m);
}
static int lcm(int m, int n)
{
return Math.Abs(m * n) / gcd(m, n);
}
static void Main()
{
Console.WriteLine("lcm(12,18)=" + lcm(12,18));
}
}

View file

@ -0,0 +1,19 @@
#include <stdio.h>
int gcd(int m, int n)
{
int tmp;
while(m) { tmp = m; m = n % m; n = tmp; }
return n;
}
int lcm(int m, int n)
{
return m / gcd(m, n) * n;
}
int main()
{
printf("lcm(35, 21) = %d\n", lcm(21,35));
return 0;
}

View file

@ -0,0 +1,17 @@
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
return(m)
end gcd
lcm = proc (m, n: int) returns (int)
if m=0 cor n=0
then return(0)
else return(int$abs(m*n) / gcd(m,n))
end
end lcm
start_up = proc ()
po: stream := stream$primary_output()
stream$putl(po, int$unparse(lcm(12, 18)))
end start_up

View file

@ -0,0 +1,63 @@
IDENTIFICATION DIVISION.
PROGRAM-ID. show-lcm.
ENVIRONMENT DIVISION.
CONFIGURATION SECTION.
REPOSITORY.
FUNCTION lcm
.
PROCEDURE DIVISION.
DISPLAY "lcm(35, 21) = " FUNCTION lcm(35, 21)
GOBACK
.
END PROGRAM show-lcm.
IDENTIFICATION DIVISION.
FUNCTION-ID. lcm.
ENVIRONMENT DIVISION.
CONFIGURATION SECTION.
REPOSITORY.
FUNCTION gcd
.
DATA DIVISION.
LINKAGE SECTION.
01 m PIC S9(8).
01 n PIC S9(8).
01 ret PIC S9(8).
PROCEDURE DIVISION USING VALUE m, n RETURNING ret.
COMPUTE ret = FUNCTION ABS(m * n) / FUNCTION gcd(m, n)
GOBACK
.
END FUNCTION lcm.
IDENTIFICATION DIVISION.
FUNCTION-ID. gcd.
DATA DIVISION.
LOCAL-STORAGE SECTION.
01 temp PIC S9(8).
01 x PIC S9(8).
01 y PIC S9(8).
LINKAGE SECTION.
01 m PIC S9(8).
01 n PIC S9(8).
01 ret PIC S9(8).
PROCEDURE DIVISION USING VALUE m, n RETURNING ret.
MOVE m to x
MOVE n to y
PERFORM UNTIL y = 0
MOVE x TO temp
MOVE y TO x
MOVE FUNCTION MOD(temp, y) TO Y
END-PERFORM
MOVE FUNCTION ABS(x) TO ret
GOBACK
.
END FUNCTION gcd.

View file

@ -0,0 +1,11 @@
(defn gcd
[a b]
(if (zero? b)
a
(recur b, (mod a b))))
(defn lcm
[a b]
(/ (* a b) (gcd a b)))
;; to calculate the lcm for a variable number of arguments
(defn lcmv [& v] (reduce lcm v))

View file

@ -0,0 +1,4 @@
CL-USER> (lcm 12 18)
36
CL-USER> (lcm 12 18 22)
396

View file

@ -0,0 +1,10 @@
CL-USER> (defun my-lcm (&rest args)
(reduce (lambda (m n)
(cond ((or (= m 0) (= n 0)) 0)
(t (abs (/ (* m n) (gcd m n))))))
args :initial-value 1))
MY-LCM
CL-USER> (my-lcm 12 18)
36
CL-USER> (my-lcm 12 18 22)
396

View file

@ -0,0 +1,21 @@
include "cowgol.coh";
sub gcd(m: uint32, n: uint32): (r: uint32) is
while n != 0 loop
var t := m;
m := n;
n := t % n;
end loop;
r := m;
end sub;
sub lcm(m: uint32, n: uint32): (r: uint32) is
if m==0 or n==0 then
r := 0;
else
r := m*n / gcd(m,n);
end if;
end sub;
print_i32(lcm(12, 18));
print_nl();

View file

@ -0,0 +1,22 @@
import std.stdio, std.bigint, std.math;
T gcd(T)(T a, T b) pure nothrow {
while (b) {
immutable t = b;
b = a % b;
a = t;
}
return a;
}
T lcm(T)(T m, T n) pure nothrow {
if (m == 0) return m;
if (n == 0) return n;
return abs((m * n) / gcd(m, n));
}
void main() {
lcm(12, 18).writeln;
lcm("2562047788015215500854906332309589561".BigInt,
"6795454494268282920431565661684282819".BigInt).writeln;
}

View file

@ -0,0 +1 @@
PrintLn(Lcm(12, 18));

View file

@ -0,0 +1,15 @@
main() {
int x=8;
int y=12;
int z= gcd(x,y);
var lcm=(x*y)/z;
print('$lcm');
}
int gcd(int a,int b)
{
if(b==0)
return a;
if(b!=0)
return gcd(b,a%b);
}

View file

@ -0,0 +1,20 @@
proc gcd(word m, n) word:
word t;
while n /= 0 do
t := m;
m := n;
n := t % n
od;
m
corp
proc lcm(word m, n) word:
if m=0 or n=0
then 0
else m*n / gcd(m,n)
fi
corp
proc main() void:
writeln(lcm(12, 18))
corp

View file

@ -0,0 +1,30 @@
PROGRAM LCM
PROCEDURE GCD(A,B->GCD)
LOCAL C
WHILE B DO
C=A
A=B
B=C MOD B
END WHILE
GCD=ABS(A)
END PROCEDURE
PROCEDURE LCM(M,N->LCM)
IF M=0 OR N=0 THEN
LCM=0
EXIT PROCEDURE
ELSE
GCD(M,N->GCD)
LCM=ABS(M*N)/GCD
END IF
END PROCEDURE
BEGIN
LCM(18,12->LCM)
PRINT("LCM of 18 AND 12 =";LCM)
LCM(14,-6->LCM)
PRINT("LCM of 14 AND -6 =";LCM)
LCM(0,35->LCM)
PRINT("LCM of 0 AND 35 =";LCM)
END PROGRAM

View file

@ -0,0 +1,6 @@
(lcm 0 9) → 0
(lcm 444 888)→ 888
(lcm 888 999) → 7992
(define (lcm* list) (foldl lcm (first list) list)) → lcm*
(lcm* '(444 888 999)) → 7992

View file

@ -0,0 +1,11 @@
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));
public program()
{
console.printLine("lcm(12,18)=",lcm(12,18))
}

View file

@ -0,0 +1,8 @@
defmodule RC do
def gcd(a,0), do: abs(a)
def gcd(a,b), do: gcd(b, rem(a,b))
def lcm(a,b), do: div(abs(a*b), gcd(a,b))
end
IO.puts RC.lcm(-12,15)

View file

@ -0,0 +1,15 @@
% Implemented by Arjun Sunel
-module(lcm).
-export([main/0]).
main() ->
lcm(-3,4).
gcd(A, 0) ->
A;
gcd(A, B) ->
gcd(B, A rem B).
lcm(A,B) ->
abs(A*B div gcd(A,B)).

View file

@ -0,0 +1,13 @@
function gcd(integer m, integer n)
integer tmp
while m do
tmp = m
m = remainder(n,m)
n = tmp
end while
return n
end function
function lcm(integer m, integer n)
return m / gcd(m, n) * n
end function

View file

@ -0,0 +1 @@
=LCM(A1:J1)

View file

@ -0,0 +1,56 @@
## இந்த நிரல் இரு எண்களுக்கு இடையிலான மீச்சிறு பொது மடங்கு (LCM), மீப்பெரு பொது வகுத்தி (GCD) என்ன என்று கணக்கிடும்
நிரல்பாகம் மீபொம(எண்1, எண்2)
@(எண்1 == எண்2) ஆனால்
## இரு எண்களும் சமம் என்பதால், மீபொம அந்த எண்ணேதான்
பின்கொடு எண்1
@(எண்1 > எண்2) இல்லைஆனால்
சிறியது = எண்2
பெரியது = எண்1
இல்லை
சிறியது = எண்1
பெரியது = எண்2
முடி
மீதம் = பெரியது % சிறியது
@(மீதம் == 0) ஆனால்
## பெரிய எண்ணில் சிறிய எண் மீதமின்றி வகுபடுவதால், பெரிய எண்தான் மீபொம
பின்கொடு பெரியது
இல்லை
தொடக்கம் = பெரியது + 1
நிறைவு = சிறியது * பெரியது
@(எண் = தொடக்கம், எண் <= நிறைவு, எண் = எண் + 1) ஆக
## ஒவ்வோர் எண்ணாக எடுத்துக்கொண்டு தரப்பட்ட இரு எண்களாலும் வகுத்துப் பார்க்கின்றோம். முதலாவதாக இரண்டாலும் மீதமின்றி வகுபடும் எண்தான் மீபொம
மீதம்1 = எண் % சிறியது
மீதம்2 = எண் % பெரியது
@((மீதம்1 == 0) && (மீதம்2 == 0)) ஆனால்
பின்கொடு எண்
முடி
முடி
முடி
முடி
அ = int(உள்ளீடு("ஓர் எண்ணைத் தாருங்கள் "))
ஆ = int(உள்ளீடு("இன்னோர் எண்ணைத் தாருங்கள் "))
பதிப்பி "நீங்கள் தந்த இரு எண்களின் மீபொம (மீச்சிறு பொது மடங்கு, LCM) = ", மீபொம(அ, ஆ)

View file

@ -0,0 +1,3 @@
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)

View file

@ -0,0 +1,2 @@
USING: math.functions prettyprint ;
26 28 lcm .

View file

@ -0,0 +1,12 @@
USING: kernel math prettyprint ;
IN: script
: gcd ( a b -- c )
[ abs ] [
[ nip ] [ mod ] 2bi gcd
] if-zero ;
: lcm ( a b -- c )
[ * abs ] [ gcd ] 2bi / ;
26 28 lcm .

View file

@ -0,0 +1 @@
Func Lecm(a,b)=|a|*|b|/GCD(a,b).

View file

@ -0,0 +1,6 @@
: 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 */ ;

View file

@ -0,0 +1,14 @@
integer function lcm(a,b)
integer:: a,b
lcm = a*b / gcd(a,b)
end function lcm
integer function gcd(a,b)
integer :: a,b,t
do while (b/=0)
t = b
b = mod(a,b)
a = t
end do
gcd = abs(a)
end function gcd

View file

@ -0,0 +1,18 @@
' FB 1.05.0 Win64
Function lcm (m As Integer, n As Integer) As Integer
If m = 0 OrElse n = 0 Then Return 0
If m < n Then Swap m, n '' to minimize iterations needed
Var count = 0
Do
count +=1
Loop Until (m * count) Mod n = 0
Return m * count
End Function
Print "lcm(12, 18) ="; lcm(12, 18)
Print "lcm(15, 12) ="; lcm(15, 12)
Print "lcm(10, 14) ="; lcm(10, 14)
Print
Print "Press any key to quit"
Sleep

View file

@ -0,0 +1,17 @@
function gcdp( a as uinteger, b as uinteger ) as uinteger
if b = 0 then return a
return gcdp( b, a mod b )
end function
function gcd(a as integer, b as integer) as uinteger
return gcdp( abs(a), abs(b) )
end function
function lcm(a as integer, b as integer) as uinteger
return abs(a*b)/gcd(a,b)
end function
print "lcm( 12, -18) = "; lcm(12, -18)
print "lcm( 15, 12) = "; lcm(15, 12)
print "lcm(-10, -14) = "; lcm(-10, -14)
print "lcm( 0, 1) = "; lcm(0,1)

View file

@ -0,0 +1 @@
println[lcm[2562047788015215500854906332309589561, 6795454494268282920431565661684282819]]

View file

@ -0,0 +1,4 @@
def
lcm( _, 0 ) = 0
lcm( 0, _ ) = 0
lcm( x, y ) = abs( (x\gcd(x, y)) y )

View file

@ -0,0 +1,3 @@
# Built-in
LcmInt(12, 18);
# 36

View file

@ -0,0 +1,22 @@
10 PRINT "LCM(35, 21) = ";
20 LET MLCM = 35
30 LET NLCM = 21
40 GOSUB 200: ' Calculate LCM
50 PRINT LCM
60 END
195 ' Calculate LCM
200 LET MGCD = MLCM
210 LET NGCD = NLCM
220 GOSUB 400: ' Calculate GCD
230 LET LCM = MLCM / GCD * NLCM
240 RETURN
395 ' Calculate GCD
400 WHILE MGCD <> 0
410 LET TMP = MGCD
420 LET MGCD = NGCD MOD MGCD
430 LET NGCD = TMP
440 WEND
450 LET GCD = NGCD
460 RETURN

View file

@ -0,0 +1,17 @@
package main
import (
"fmt"
"math/big"
)
var m, n, z big.Int
func init() {
m.SetString("2562047788015215500854906332309589561", 10)
n.SetString("6795454494268282920431565661684282819", 10)
}
func main() {
fmt.Println(z.Mul(z.Div(&m, z.GCD(nil, nil, &m, &n)), &n))
}

View file

@ -0,0 +1,11 @@
def gcd
gcd = { m, n -> m = m.abs(); n = n.abs(); n == 0 ? m : m%n == 0 ? n : gcd(n, m % n) }
def lcd = { m, n -> Math.abs(m * n) / gcd(m, n) }
[[m: 12, n: 18, l: 36],
[m: -6, n: 14, l: 42],
[m: 35, n: 0, l: 0]].each { t ->
println "LCD of $t.m, $t.n is $t.l"
assert lcd(t.m, t.n) == t.l
}

View file

@ -0,0 +1,4 @@
lcm :: (Integral a) => a -> a -> a
lcm _ 0 = 0
lcm 0 _ = 0
lcm x y = abs ((x `quot` (gcd x y)) * y)

View file

@ -0,0 +1,8 @@
100 DEF LCM(A,B)=(A*B)/GCD(A,B)
110 DEF GCD(A,B)
120 DO WHILE B>0
130 LET T=B:LET B=MOD(A,B):LET A=T
140 LOOP
150 LET GCD=A
160 END DEF
170 PRINT LCM(12,18)

View file

@ -0,0 +1,5 @@
link numbers
procedure main()
write("lcm of 18, 36 = ",lcm(18,36))
write("lcm of 0, 9 = ",lcm(0,9))
end

View file

@ -0,0 +1,4 @@
procedure lcm(i, j) #: least common multiple
if (i = 0) | (j = 0) then return 0
return abs(i * j) / gcd(i, j)
end

View file

@ -0,0 +1,11 @@
12 *. 18
36
12 *. 18 22
36 132
*./ 12 18 22
396
0 1 0 1 *. 0 0 1 1 NB. for truth valued arguments (0 and 1) it is equivalent to "and"
0 0 0 1
*./~ 0 1
0 0
0 1

View file

@ -0,0 +1,27 @@
import java.util.Scanner;
public class LCM{
public static void main(String[] args){
Scanner aScanner = new Scanner(System.in);
//prompts user for values to find the LCM for, then saves them to m and n
System.out.print("Enter the value of m:");
int m = aScanner.nextInt();
System.out.print("Enter the value of n:");
int n = aScanner.nextInt();
int lcm = (n == m || n == 1) ? m :(m == 1 ? n : 0);
/* this section increases the value of mm until it is greater
/ than or equal to nn, then does it again when the lesser
/ becomes the greater--if they aren't equal. If either value is 1,
/ no need to calculate*/
if (lcm == 0) {
int mm = m, nn = n;
while (mm != nn) {
while (mm < nn) { mm += m; }
while (nn < mm) { nn += n; }
}
lcm = mm;
}
System.out.println("lcm(" + m + ", " + n + ") = " + lcm);
}
}

View file

@ -0,0 +1,14 @@
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]);
for (var i = 1; i < n; i++)
{ var b = Math.abs(A[i]), c = a;
while (a && b){ a > b ? a %= b : b %= a; }
a = Math.abs(c*A[i])/(a+b);
}
return a;
}
/* For example:
LCM([-50,25,-45,-18,90,447]) -> 67050
*/

View file

@ -0,0 +1,18 @@
(() => {
'use strict';
// gcd :: Integral a => a -> a -> a
let gcd = (x, y) => {
let _gcd = (a, b) => (b === 0 ? a : _gcd(b, a % b)),
abs = Math.abs;
return _gcd(abs(x), abs(y));
}
// lcm :: Integral a => a -> a -> a
let lcm = (x, y) =>
x === 0 || y === 0 ? 0 : Math.abs(Math.floor(x / gcd(x, y)) * y);
// TEST
return lcm(12, 18);
})();

View file

@ -0,0 +1,6 @@
# 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;

View file

@ -0,0 +1 @@
lcm(m,n)

View file

@ -0,0 +1,7 @@
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

View file

@ -0,0 +1,8 @@
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

View file

@ -0,0 +1,16 @@
:gcd { u v -- n }
abs int swap abs int swap
[over over mod rot drop]
[dup]
while
drop
;
:lcm { m n -- n }
over over gcd rot swap div mult
;
12 18 lcm print nl { 36 }
"End " input

View file

@ -0,0 +1,5 @@
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))
}

View file

@ -0,0 +1,19 @@
define gcd(a,b) => {
while(#b != 0) => {
local(t = #b)
#b = #a % #b
#a = #t
}
return #a
}
define lcm(m,n) => {
#m == 0 || #n == 0 ? return 0
local(r = (#m * #n) / decimal(gcd(#m, #n)))
return integer(#r)->abs
}
lcm(-6, 14)
lcm(2, 0)
lcm(12, 18)
lcm(12, 22)
lcm(7, 31)

View file

@ -0,0 +1,15 @@
print "Least Common Multiple of 12 and 18 is "; LCM(12, 18)
end
function LCM(m, n)
LCM = abs(m * n) / GCD(m, n)
end function
function GCD(a, b)
while b
c = a
a = b
b = c mod b
wend
GCD = abs(a)
end function

View file

@ -0,0 +1,11 @@
to abs :n
output sqrt product :n :n
end
to gcd :m :n
output ifelse :n = 0 [ :m ] [ gcd :n modulo :m :n ]
end
to lcm :m :n
output quotient (abs product :m :n) gcd :m :n
end

View file

@ -0,0 +1 @@
print lcm 38 46

View file

@ -0,0 +1,14 @@
function gcd( m, n )
while n ~= 0 do
local q = m
m = n
n = q % n
end
return m
end
function lcm( m, n )
return ( m ~= 0 and n ~= 0 ) and m * n / gcd( m, n ) or 0
end
print( lcm(12,18) )

View file

@ -0,0 +1,20 @@
divert(-1)
define(`gcd',
`ifelse(eval(`0 <= (' $1 `)'),`0',`gcd(eval(`-(' $1 `)'),eval(`(' $2 `)'))',
eval(`0 <= (' $2 `)'),`0',`gcd(eval(`(' $1 `)'),eval(`-(' $2 `)'))',
eval(`(' $1 `) == 0'),`0',`gcd(eval(`(' $2 `) % (' $1 `)'),eval(`(' $1 `)'))',
eval(`(' $2 `)'))')
define(`lcm',
`ifelse(eval(`0 <= (' $1 `)'),`0',`lcm(eval(`-(' $1 `)'),eval(`(' $2 `)'))',
eval(`0 <= (' $2 `)'),`0',`lcm(eval(`(' $1 `)'),eval(`-(' $2 `)'))',
eval(`(' $1 `) == 0'),`0',`eval(`(' $1 `) * (' $2 `) /' gcd(eval(`(' $1 `)'),eval(`(' $2 `)')))')')
divert`'dnl
dnl
lcm(-6, 14) = 42
lcm(2, 0) = 0
lcm(12, 18) = 36
lcm(12, 22) = 132
lcm(7, 31) = 217

View file

@ -0,0 +1 @@
lcm(a,b)

View file

@ -0,0 +1,9 @@
fun gcd (a, 0) = a
| (0, b) = b
| (a, b) where (a < b)
= gcd (a, b rem a)
| (a, b) = gcd (b, a rem b)
fun lcm (a, b) = let val d = gcd (a, b)
in a * b div d
end

View file

@ -0,0 +1,2 @@
> ilcm( 12, 18 );
36

View file

@ -0,0 +1,2 @@
LCM[18,12]
-> 36

View file

@ -0,0 +1,6 @@
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))

View file

@ -0,0 +1,21 @@
Textwindow.Write("LCM(35, 21) = ")
mlcm = 35
nlcm = 21
CalculateLCM()
TextWindow.WriteLine(lcm)
Sub CalculateLCM
mgcd = mlcm
ngcd = nlcm
CalculateGCD()
lcm = mlcm / gcd * nlcm
EndSub
Sub CalculateGCD
While mgcd <> 0
tmp = mgcd
mgcd = Math.Remainder(ngcd, mgcd)
ngcd = tmp
EndWhile
gcd = ngcd
EndSub

View file

@ -0,0 +1,3 @@
((0 <) (-1 *) when) :abs
((dup 0 ==) (pop abs) (swap over mod) () linrec) :gcd
(over over gcd '* dip div) :lcm

View file

@ -0,0 +1,15 @@
gcd = function(a, b)
while b
temp = b
b = a % b
a = temp
end while
return abs(a)
end function
lcm = function(a,b)
if not a and not b then return 0
return abs(a * b) / gcd(a, b)
end function
print lcm(18,12)

View file

@ -0,0 +1,20 @@
function var int: lcm(int: a2,int:b2) =
let {
int:a1 = max(a2,b2);
int:b1 = min(a2,b2);
array[0..a1,0..b1] of var int: gcd;
constraint forall(a in 0..a1)(
forall(b in 0..b1)(
gcd[a,b] ==
if (b == 0) then
a
else
gcd[b, a mod b]
endif
)
)
} in (a1*b1) div gcd[a1,b1];
var int: lcm1 = lcm(18,12);
solve satisfy;
output [show(lcm1),"\n"];

View file

@ -0,0 +1,29 @@
MODULE LeastCommonMultiple;
FROM STextIO IMPORT
WriteString, WriteLn;
FROM SWholeIO IMPORT
WriteInt;
PROCEDURE GCD(M, N: INTEGER): INTEGER;
VAR
Tmp: INTEGER;
BEGIN
WHILE M <> 0 DO
Tmp := M;
M := N MOD M;
N := Tmp;
END;
RETURN N;
END GCD;
PROCEDURE LCM(M, N: INTEGER): INTEGER;
BEGIN
RETURN M / GCD(M, N) * N;
END LCM;
BEGIN
WriteString("LCM(35, 21) = ");
WriteInt(LCM(35, 21), 1);
WriteLn;
END LeastCommonMultiple.

View file

@ -0,0 +1,22 @@
def gcd(a, b)
if (a < 1) or (b < 1)
throw new(InvalidNumberException, "gcd cannot be calculated on values < 1")
end
c = 0
while b != 0
c = a
a = b
b = c % b
end
return a
end
def lcm(m, n)
return (m * n) / gcd(m, n)
end
println lcm(12, 18)
println lcm(6, 14)
println lcm(1,2) = lcm(2,1)

View file

@ -0,0 +1,70 @@
/* NetRexx */
options replace format comments java crossref symbols nobinary
numeric digits 3000
runSample(arg)
return
-- ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
method lcm(m_, n_) public static
L_ = m_ * n_ % gcd(m_, n_)
return L_
-- ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
-- Euclid's algorithm - iterative implementation
method gcd(m_, n_) public static
loop while n_ > 0
c_ = m_ // n_
m_ = n_
n_ = c_
end
return m_
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
method runSample(arg) private static
parse arg samples
if samples = '' | samples = '.' then
samples = '-6 14 = 42 |' -
'3 4 = 12 |' -
'18 12 = 36 |' -
'2 0 = 0 |' -
'0 85 = 0 |' -
'12 18 = 36 |' -
'5 12 = 60 |' -
'12 22 = 132 |' -
'7 31 = 217 |' -
'117 18 = 234 |' -
'38 46 = 874 |' -
'18 12 -5 = 180 |' -
'-5 18 12 = 180 |' - -- confirm that other permutations work
'12 -5 18 = 180 |' -
'18 12 -5 97 = 17460 |' -
'30 42 = 210 |' -
'30 42 = . |' - -- 210; no verification requested
'18 12' -- 36
loop while samples \= ''
parse samples sample '|' samples
loop while sample \= ''
parse sample mnvals '=' chk sample
if chk = '' then chk = '.'
mv = mnvals.word(1)
loop w_ = 2 to mnvals.words mnvals
nv = mnvals.word(w_)
mv = mv.abs
nv = nv.abs
mv = lcm(mv, nv)
end w_
lv = mv
select case chk
when '.' then state = ''
when lv then state = '(verified)'
otherwise state = '(failed)'
end
mnvals = mnvals.space(1, ',').changestr(',', ', ')
say 'lcm of' mnvals.right(15.max(mnvals.length)) 'is' lv.right(5.max(lv.length)) state
end
end
return

Some files were not shown because too many files have changed in this diff Show more