A-M baby
This commit is contained in:
parent
764da6cbbb
commit
db842d013d
19005 changed files with 197040 additions and 7 deletions
13
Task/Least-common-multiple/0DESCRIPTION
Normal file
13
Task/Least-common-multiple/0DESCRIPTION
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
Compute the least common multiple of two integers.
|
||||
|
||||
Given ''m'' and ''n'', the least common multiple is the smallest positive integer that has both ''m'' and ''n'' as factors. For 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''.
|
||||
|
||||
<math>\operatorname{lcm}(m, n) = \frac{|m \times n|}{\operatorname{gcd}(m, n)}</math>
|
||||
|
||||
One can also find ''lcm'' by merging the [[prime decomposition]]s of both ''m'' and ''n''.
|
||||
|
||||
References: [http://mathworld.wolfram.com/LeastCommonMultiple.html MathWorld], [[wp:Least common multiple|Wikipedia]].
|
||||
22
Task/Least-common-multiple/AWK/least-common-multiple.awk
Normal file
22
Task/Least-common-multiple/AWK/least-common-multiple.awk
Normal 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) }
|
||||
28
Task/Least-common-multiple/Ada/least-common-multiple.ada
Normal file
28
Task/Least-common-multiple/Ada/least-common-multiple.ada
Normal 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;
|
||||
|
|
@ -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)
|
||||
|
|
@ -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%)
|
||||
|
|
@ -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))
|
||||
9
Task/Least-common-multiple/C++/least-common-multiple.cpp
Normal file
9
Task/Least-common-multiple/C++/least-common-multiple.cpp
Normal 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 ;
|
||||
}
|
||||
19
Task/Least-common-multiple/C/least-common-multiple.c
Normal file
19
Task/Least-common-multiple/C/least-common-multiple.c
Normal 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;
|
||||
}
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
(defn gcd
|
||||
[a b]
|
||||
(if (zero? b)
|
||||
a
|
||||
(recur b, (mod a b))))
|
||||
|
||||
(defn lcm
|
||||
[a b]
|
||||
(/ (* a b) (gcd a b)))
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
CL-USER> (lcm 12 18)
|
||||
36
|
||||
CL-USER> (lcm 12 18 22)
|
||||
396
|
||||
|
|
@ -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
|
||||
24
Task/Least-common-multiple/D/least-common-multiple.d
Normal file
24
Task/Least-common-multiple/D/least-common-multiple.d
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
import std.stdio, std.bigint;
|
||||
|
||||
T lcm(T)(/*in*/ T m, /*in*/ T n) /*pure nothrow*/ {
|
||||
if (m == 0) return m;
|
||||
if (n == 0) return n;
|
||||
T r = (m * n) / gcd(m, n);
|
||||
//return abs(r);
|
||||
return (r < 0) ? -r : r;
|
||||
}
|
||||
|
||||
T gcd(T)(/*in*/ T a, /*in*/ T b) /*pure nothrow*/ {
|
||||
while (b != 0) {
|
||||
auto t = b;
|
||||
b = a % b;
|
||||
a = t;
|
||||
}
|
||||
return a;
|
||||
}
|
||||
|
||||
void main() {
|
||||
writeln(lcm(12, 18));
|
||||
writeln(lcm(BigInt("2562047788015215500854906332309589561"),
|
||||
BigInt("6795454494268282920431565661684282819")));
|
||||
}
|
||||
|
|
@ -0,0 +1 @@
|
|||
PrintLn(Lcm(12, 18));
|
||||
|
|
@ -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
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
USING: math.functions prettyprint ;
|
||||
26 28 lcm .
|
||||
|
|
@ -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 .
|
||||
|
|
@ -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 */ ;
|
||||
|
|
@ -0,0 +1 @@
|
|||
println[lcm[2562047788015215500854906332309589561, 6795454494268282920431565661684282819]]
|
||||
3
Task/Least-common-multiple/GAP/least-common-multiple.gap
Normal file
3
Task/Least-common-multiple/GAP/least-common-multiple.gap
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
# Built-in
|
||||
LcmInt(12, 18);
|
||||
# 36
|
||||
17
Task/Least-common-multiple/Go/least-common-multiple.go
Normal file
17
Task/Least-common-multiple/Go/least-common-multiple.go
Normal 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))
|
||||
}
|
||||
|
|
@ -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)
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
link numbers
|
||||
procedure main()
|
||||
write("lcm of 18, 36 = ",lcm(18,36))
|
||||
write("lcm of 0, 9 36 = ",lcm(0,9))
|
||||
end
|
||||
|
|
@ -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
|
||||
11
Task/Least-common-multiple/J/least-common-multiple.j
Normal file
11
Task/Least-common-multiple/J/least-common-multiple.j
Normal 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 boolean arguments (0 and 1) it is equivalent to "and"
|
||||
0 0 0 1
|
||||
*./~ 0 1
|
||||
0 0
|
||||
0 1
|
||||
27
Task/Least-common-multiple/Java/least-common-multiple.java
Normal file
27
Task/Least-common-multiple/Java/least-common-multiple.java
Normal 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);
|
||||
}
|
||||
}
|
||||
8
Task/Least-common-multiple/K/least-common-multiple.k
Normal file
8
Task/Least-common-multiple/K/least-common-multiple.k
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
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
|
||||
|
|
@ -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
|
||||
11
Task/Least-common-multiple/Logo/least-common-multiple-1.logo
Normal file
11
Task/Least-common-multiple/Logo/least-common-multiple-1.logo
Normal 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
|
||||
|
|
@ -0,0 +1 @@
|
|||
print lcm 38 46
|
||||
14
Task/Least-common-multiple/Lua/least-common-multiple.lua
Normal file
14
Task/Least-common-multiple/Lua/least-common-multiple.lua
Normal 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) )
|
||||
|
|
@ -0,0 +1 @@
|
|||
lcm(a,b)
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
> ilcm( 12, 18 );
|
||||
36
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
LCM[18,12]
|
||||
-> 36
|
||||
|
|
@ -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))
|
||||
16
Task/Least-common-multiple/PHP/least-common-multiple.php
Normal file
16
Task/Least-common-multiple/PHP/least-common-multiple.php
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
echo lcm(12, 18) == 36;
|
||||
|
||||
function lcm($m, $n) {
|
||||
if ($m == 0 || $n == 0) return 0;
|
||||
$r = ($m * $n) / gcd($m, $n);
|
||||
return abs($r);
|
||||
}
|
||||
|
||||
function gcd($a, $b) {
|
||||
while ($b != 0) {
|
||||
$t = $b;
|
||||
$b = $a % $b;
|
||||
$a = $t;
|
||||
}
|
||||
return $a;
|
||||
}
|
||||
12
Task/Least-common-multiple/Perl/least-common-multiple-1.pl
Normal file
12
Task/Least-common-multiple/Perl/least-common-multiple-1.pl
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
sub gcd {
|
||||
my ($a, $b) = @_;
|
||||
while ($a) { ($a, $b) = ($b % $a, $a) }
|
||||
$b
|
||||
}
|
||||
|
||||
sub lcm {
|
||||
my ($a, $b) = @_;
|
||||
($a && $b) and $a / gcd($a, $b) * $b or 0
|
||||
}
|
||||
|
||||
print lcm(1001, 221);
|
||||
13
Task/Least-common-multiple/Perl/least-common-multiple-2.pl
Normal file
13
Task/Least-common-multiple/Perl/least-common-multiple-2.pl
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
sub lcm {
|
||||
use integer;
|
||||
my ($x, $y) = @_;
|
||||
my ($a, $b) = @_;
|
||||
while ($a != $b) {
|
||||
($a, $b, $x, $y) = ($b, $a, $y, $x) if $a > $b;
|
||||
$a = $b / $x * $x;
|
||||
$a += $x if $a < $b;
|
||||
}
|
||||
$a
|
||||
}
|
||||
|
||||
print lcm(1001, 221);
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
(de lcm (A B)
|
||||
(abs (*/ A B (gcd A B))) )
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
lcm(X, Y, Z) :-
|
||||
Z is abs(X * Y) / gcd(X,Y).
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
>>> import fractions
|
||||
>>> def lcm(a,b): return abs(a * b) / fractions.gcd(a,b) if a and b else 0
|
||||
|
||||
>>> lcm(12, 18)
|
||||
36
|
||||
>>> lcm(-6, 14)
|
||||
42
|
||||
>>> assert lcm(0, 2) == lcm(2, 0) == 0
|
||||
>>>
|
||||
18
Task/Least-common-multiple/Python/least-common-multiple-2.py
Normal file
18
Task/Least-common-multiple/Python/least-common-multiple-2.py
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
import operator
|
||||
from prime_decomposition import decompose
|
||||
|
||||
def lcm(a, b):
|
||||
if a and b:
|
||||
da = list(decompose(abs(a)))
|
||||
db = list(decompose(abs(b)))
|
||||
merge= da
|
||||
for d in da:
|
||||
if d in db: db.remove(d)
|
||||
merge += db
|
||||
return reduce(operator.mul, merge, 1)
|
||||
return 0
|
||||
|
||||
if __name__ == '__main__':
|
||||
print( lcm(12, 18) ) # 36
|
||||
print( lcm(-6, 14) ) # 42
|
||||
assert lcm(0, 2) == lcm(2, 0) == 0
|
||||
19
Task/Least-common-multiple/Python/least-common-multiple-3.py
Normal file
19
Task/Least-common-multiple/Python/least-common-multiple-3.py
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
>>> def lcm(*values):
|
||||
values = set([abs(int(v)) for v in values])
|
||||
if values and 0 not in values:
|
||||
n = n0 = max(values)
|
||||
values.remove(n)
|
||||
while any( n % m for m in values ):
|
||||
n += n0
|
||||
return n
|
||||
return 0
|
||||
|
||||
>>> lcm(-6, 14)
|
||||
42
|
||||
>>> lcm(2, 0)
|
||||
0
|
||||
>>> lcm(12, 18)
|
||||
36
|
||||
>>> lcm(12, 18, 22)
|
||||
396
|
||||
>>>
|
||||
18
Task/Least-common-multiple/Python/least-common-multiple-4.py
Normal file
18
Task/Least-common-multiple/Python/least-common-multiple-4.py
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
>>> def lcm(p,q):
|
||||
p, q = abs(p), abs(q)
|
||||
m = p * q
|
||||
if not m: return 0
|
||||
while True:
|
||||
p %= q
|
||||
if not p: return m // q
|
||||
q %= p
|
||||
if not q: return m // p
|
||||
|
||||
|
||||
>>> lcm(-6, 14)
|
||||
42
|
||||
>>> lcm(12, 18)
|
||||
36
|
||||
>>> lcm(2, 0)
|
||||
0
|
||||
>>>
|
||||
27
Task/Least-common-multiple/REXX/least-common-multiple.rexx
Normal file
27
Task/Least-common-multiple/REXX/least-common-multiple.rexx
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
/*REXX pgm finds LCM (Least Common Multiple) of a number of integers.*/
|
||||
numeric digits 9000 /*handle nine-thousand digit nums*/
|
||||
|
||||
say 'the LCM of 19 & 0 is:' lcm(19 0)
|
||||
say 'the LCM of 0 & 85 is:' lcm( 0 85)
|
||||
say 'the LCM of 14 & -6 is:' lcm(14,-6)
|
||||
say 'the LCM of 18 & 12 is:' lcm(18 12)
|
||||
say 'the LCM of 18 & 12 & -5 is:' lcm(18 12,-5)
|
||||
say 'the LCM of 18 & 12 & -5 & 97 is:' lcm(18,12,-5,97)
|
||||
say 'the LCM of 2**19-1 & 2**521-1 is:' lcm(2**19-1 2**521-1)
|
||||
/*──(above)── the 7th and 13th Mersenne primes.*/
|
||||
exit /*stick a fork in it, we're done.*/
|
||||
/*──────────────────────────────────LCM subroutine──────────────────────*/
|
||||
lcm: procedure; $=; do j=1 for arg(); $=$ arg(j); end
|
||||
x=abs(word($,1)) /* [↑] build a list of arguments.*/
|
||||
do k=2 to words($); !=abs(word($,k)); if !=0 then return 0
|
||||
x=x*! / gcd(x,!) /*have GCD do the heavy lifting.*/
|
||||
end /*k*/
|
||||
return x /*return with the money. */
|
||||
/*──────────────────────────────────GCD subroutine──────────────────────*/
|
||||
gcd: procedure; $=; do j=1 for arg(); $=$ arg(j); end
|
||||
parse var $ x z .; if x=0 then x=z /* [↑] build a list of arguments.*/
|
||||
x=abs(x)
|
||||
do k=2 to words($); y=abs(word($,k)); if y=0 then iterate
|
||||
do until _==0; _=x//y; x=y; y=_; end /*until*/
|
||||
end /*k*/
|
||||
return x /*return with the money. */
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
irb(main):001:0> require 'rational'
|
||||
=> true
|
||||
irb(main):002:0> 12.lcm 18
|
||||
=> 36
|
||||
10
Task/Least-common-multiple/Ruby/least-common-multiple-2.rb
Normal file
10
Task/Least-common-multiple/Ruby/least-common-multiple-2.rb
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
def lcm(*args)
|
||||
args.inject(1) do |m, n|
|
||||
next 0 if m == 0 or n == 0
|
||||
i = m
|
||||
loop do
|
||||
break i if i % n == 0
|
||||
i += m
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
irb(main):004:0> lcm 12, 18
|
||||
=> 36
|
||||
irb(main):005:0> lcm 12, 18, 22
|
||||
=> 396
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
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)
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
lcm(12, 18) // 36
|
||||
lcm( 2, 0) // 0
|
||||
lcm(-6, 14) // 42
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
> (lcm 108 8)
|
||||
216
|
||||
10
Task/Least-common-multiple/Tcl/least-common-multiple-1.tcl
Normal file
10
Task/Least-common-multiple/Tcl/least-common-multiple-1.tcl
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
proc lcm {p q} {
|
||||
set m [expr {$p * $q}]
|
||||
if {!$m} {return 0}
|
||||
while 1 {
|
||||
set p [expr {$p % $q}]
|
||||
if {!$p} {return [expr {$m / $q}]}
|
||||
set q [expr {$q % $p}]
|
||||
if {!$q} {return [expr {$m / $p}]}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1 @@
|
|||
puts [lcm 12 18]
|
||||
Loading…
Add table
Add a link
Reference in a new issue