2016 Update
This commit is contained in:
parent
948b86eafa
commit
dcf5d15da3
7965 changed files with 139854 additions and 31002 deletions
|
|
@ -1,13 +1,24 @@
|
|||
;Task:
|
||||
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.
|
||||
Given ''m'' and ''n'', the least common multiple is the smallest positive integer that has both ''m'' and ''n'' as factors.
|
||||
|
||||
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''.
|
||||
;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.
|
||||
|
||||
<math>\operatorname{lcm}(m, n) = \frac{|m \times n|}{\operatorname{gcd}(m, n)}</math>
|
||||
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''.
|
||||
|
||||
One can also find ''lcm'' by merging the [[prime decomposition]]s of both ''m'' and ''n''.
|
||||
If you already have ''gcd'' for [[greatest common divisor]], then this formula calculates ''lcm''.
|
||||
|
||||
References: [http://mathworld.wolfram.com/LeastCommonMultiple.html MathWorld], [[wp:Least common multiple|Wikipedia]].
|
||||
<big>
|
||||
:::: <math>\operatorname{lcm}(m, n) = \frac{|m \times n|}{\operatorname{gcd}(m, n)}</math>
|
||||
</big>
|
||||
|
||||
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]].
|
||||
<br><br>
|
||||
|
|
|
|||
|
|
@ -0,0 +1,2 @@
|
|||
12^18
|
||||
36
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
LCM←{(|⍺×⍵)÷⍺∨⍵}
|
||||
12 LCM 18
|
||||
36
|
||||
|
|
@ -0,0 +1,44 @@
|
|||
-- lcm :: Integral a => a -> a -> a
|
||||
on lcm(x, y)
|
||||
if x = 0 or y = 0 then
|
||||
0
|
||||
else
|
||||
abs(x div (gcd(x, y)) * y)
|
||||
end if
|
||||
end lcm
|
||||
|
||||
|
||||
-- TEST
|
||||
on run
|
||||
|
||||
lcm(12, 18)
|
||||
|
||||
--> 36
|
||||
end run
|
||||
|
||||
|
||||
-- GENERAL FUNCTIONS
|
||||
|
||||
-- abs :: Num a => a -> a
|
||||
on abs(x)
|
||||
if x < 0 then
|
||||
-x
|
||||
else
|
||||
x
|
||||
end if
|
||||
end abs
|
||||
|
||||
-- gcd :: Integral a => a -> a -> a
|
||||
on gcd(x, y)
|
||||
script _gcd
|
||||
on lambda(a, b)
|
||||
if b = 0 then
|
||||
a
|
||||
else
|
||||
lambda(b, a mod b)
|
||||
end if
|
||||
end lambda
|
||||
end script
|
||||
|
||||
_gcd's lambda(abs(x), abs(y))
|
||||
end gcd
|
||||
|
|
@ -0,0 +1 @@
|
|||
36
|
||||
12
Task/Least-common-multiple/Brat/least-common-multiple.brat
Normal file
12
Task/Least-common-multiple/Brat/least-common-multiple.brat
Normal 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
|
||||
|
|
@ -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);
|
||||
|
||||
})();
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
fun gcd(a: Int, b: Int): Int = if (b == 0) a else gcd(b, a % b)
|
||||
|
||||
fun lcm(a: Int, b: Int) = a * b / gcd(a, b)
|
||||
|
|
@ -1,24 +1,24 @@
|
|||
/*REXX pgm finds LCM (Least Common Multiple) of any number of integers.*/
|
||||
numeric digits 10000 /*can handle 10,000 digit numbers*/
|
||||
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)
|
||||
/* [↑] 7th, 13th Mersenne primes*/
|
||||
exit /*stick a fork in it, we're done.*/
|
||||
/*──────────────────────────────────LCM subroutine──────────────────────*/
|
||||
lcm: procedure; parse arg $,_; $=$ _; do i=3 to arg(); $=$ arg(i); end
|
||||
parse var $ x $ /*obtain the first value in args.*/
|
||||
x=abs(x) /*use the absolute value of X. */
|
||||
do while $\=='' /*process the remainder of args. */
|
||||
parse var $ ! $; !=abs(!) /*pick off the next arg (ABS val)*/
|
||||
if !==0 then return 0 /*if zero, then LCM is also zero.*/
|
||||
d=x*! /*calculate part of the LCM here */
|
||||
do until !==0; parse value x//! ! with ! x
|
||||
end /*until*/ /* [↑] this is a short&fast GCD.*/
|
||||
x=d%x /*divide the pre─calculated value*/
|
||||
end /*while*/ /* [↑] process subsequent args. */
|
||||
return x /*return with the LCM of the args*/
|
||||
/*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 )
|
||||
say 'the LCM of 0 and 85 is ───► ' lcm( 0 85 )
|
||||
say 'the LCM of 14 and -6 is ───► ' lcm(14, -6 )
|
||||
say 'the LCM of 18 and 12 is ───► ' lcm(18 12 )
|
||||
say 'the LCM of 18 and 12 and -5 is ───► ' lcm(18 12, -5 )
|
||||
say 'the LCM of 18 and 12 and -5 and 97 is ───► ' lcm(18, 12, -5, 97)
|
||||
say 'the LCM of 2**19-1 and 2**521-1 is ───► ' lcm(2**19-1 2**521-1)
|
||||
/* [↑] 7th & 13th Mersenne primes.*/
|
||||
exit /*stick a fork in it, we're all done. */
|
||||
/*──────────────────────────────────────────────────────────────────────────────────────*/
|
||||
lcm: procedure; parse arg $,_; $=$ _; do i=3 to arg(); $=$ arg(i); end /*i*/
|
||||
parse var $ x $ /*obtain the first value in args. */
|
||||
x=abs(x) /*use the absolute value of X. */
|
||||
do while $\=='' /*process the remainder of args. */
|
||||
parse var $ ! $; !=abs(!) /*pick off the next arg (ABS val).*/
|
||||
if !==0 then return 0 /*if zero, then LCM is also zero. */
|
||||
d=x*! /*calculate part of the LCM here. */
|
||||
do until !==0; parse value x//! ! with ! x
|
||||
end /*until*/ /* [↑] this is a short & fast GCD*/
|
||||
x=d%x /*divide the pre─calculated value.*/
|
||||
end /*while*/ /* [↑] process subsequent args. */
|
||||
return x /*return with the LCM of the args.*/
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue