June 2018 Update

This commit is contained in:
Ingy döt Net 2018-06-22 20:57:24 +00:00
parent ba8067c3b7
commit 22f33d4004
5278 changed files with 84726 additions and 14379 deletions

View file

@ -18,7 +18,7 @@ If you already have   ''gcd''   for [[greatest common divisor]], &nbsp
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]].
;See also:
*   MathWorld entry:   [http://mathworld.wolfram.com/LeastCommonMultiple.html Least Common Multiple].
*   Wikipedia entry:   [[wp:Least common multiple|Least common multiple]].
<br><br>

View file

@ -2,13 +2,11 @@
#include <iostream>
#include <tuple>
using namespace std;
int gcd(int a, int b) {
a = abs(a);
b = abs(b);
while (b != 0) {
tie(a, b) = make_tuple(b, a % b);
std::tie(a, b) = std::make_tuple(b, a % b);
}
return a;
}
@ -19,8 +17,8 @@ int lcm(int a, int b) {
}
int main() {
cout << "The least common multiple of 12 and 18 is " << lcm(12, 18)
<< " ,\n"
<< "and the greatest common divisor " << gcd(12, 18) << " !" << endl;
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,11 @@
import extensions.
import system'math.
gcd = (:m:n)((n == 0)iif(m absolute, $(gcd(n,n mod:m)))).
lcm = (:m:n)((m * n) absolute / gcd(m,n)).
program =
[
console printLine("lcm(12,18)=",lcm(12,18)).
].

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,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

@ -1,12 +1,12 @@
sub gcd {
my ($a, $b) = @_;
while ($a) { ($a, $b) = ($b % $a, $a) }
$b
my ($x, $y) = @_;
while ($x) { ($x, $y) = ($y % $x, $x) }
$y
}
sub lcm {
my ($a, $b) = @_;
($a && $b) and $a / gcd($a, $b) * $b or 0
my ($x, $y) = @_;
($x && $y) and $x / gcd($x, $y) * $y or 0
}
print lcm(1001, 221);

View file

@ -1,13 +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;
my ($f, $s) = @_;
while ($f != $s) {
($f, $s, $x, $y) = ($s, $f, $y, $x) if $f > $s;
$f = $s / $x * $x;
$f += $x if $f < $s;
}
$a
$f
}
print lcm(1001, 221);

View file

@ -14,7 +14,7 @@ lcm: procedure; parse arg $,_; $=$ _; do i=3 to arg(); $=$ arg(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).*/
parse var $ ! $; if !<0 then !=-! /*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

View file

@ -1,16 +1,23 @@
use std::cmp::{min, max};
fn gcd_stein(a: usize, b: usize) -> usize {
use std::cmp::{max, min};
fn gcd(a: usize, b: usize) -> usize {
match ((a, b), (a & 1, b & 1)) {
((x, y), _) if x == y => y,
((0, x), _) | ((x, 0), _) => x,
((x, y), _) if x == y => y,
((0, x), _) | ((x, 0), _) => x,
((x, y), (0, 1)) | ((y, x), (1, 0)) => gcd(x >> 1, y),
((x, y), (0, 0)) => gcd(x >> 1, y >> 1) << 1,
((x, y), (1, 1)) => { let (x, y) = (min(x, y), max(x, y));
gcd((y - x) >> 1, x)
}
_ => unreachable!(),
((x, y), (0, 0)) => gcd(x >> 1, y >> 1) << 1,
((x, y), (1, 1)) => {
let (x, y) = (min(x, y), max(x, y));
gcd((y - x) >> 1, x)
}
_ => unreachable!(),
}
}
fn lcm(a: usize, b: usize) -> usize {
a * b / gcd_stein(a,b)
a * b / gcd(a, b)
}
fn main() {
println!("{}", lcm(6324, 234))
}