September 2017 Update

This commit is contained in:
Ingy döt Net 2017-09-23 10:01:46 +02:00
parent bba7bfd280
commit ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions

View file

@ -1,3 +1,5 @@
-- LEAST COMMON MULTIPLE -----------------------------------------------------
-- lcm :: Integral a => a -> a -> a
on lcm(x, y)
if x = 0 or y = 0 then
@ -8,7 +10,7 @@ on lcm(x, y)
end lcm
-- TEST
-- TEST ----------------------------------------------------------------------
on run
lcm(12, 18)
@ -16,8 +18,7 @@ on run
--> 36
end run
-- GENERAL FUNCTIONS
-- GENERIC FUNCTIONS ---------------------------------------------------------
-- abs :: Num a => a -> a
on abs(x)
@ -30,15 +31,15 @@ end abs
-- gcd :: Integral a => a -> a -> a
on gcd(x, y)
script _gcd
on lambda(a, b)
script
on |λ|(a, b)
if b = 0 then
a
else
lambda(b, a mod b)
|λ|(b, a mod b)
end if
end lambda
end |λ|
end script
_gcd's lambda(abs(x), abs(y))
result's |λ|(abs(x), abs(y))
end gcd

View file

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

@ -1,3 +0,0 @@
12 3 5 23 13 67 15 9 4 2
3605940

View file

@ -1,3 +1,5 @@
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)
fun main(args: Array<String>) {
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)
println(lcm(15, 9))
}

View file

@ -0,0 +1,18 @@
say lcm(18, 12)
-- calculate the greatest common denominator of a numerator/denominator pair
::routine gcd private
use arg x, y
loop while y \= 0
-- check if they divide evenly
temp = x // y
x = y
y = temp
end
return x
-- calculate the least common multiple of a numerator/denominator pair
::routine lcm private
use arg x, y
return x / gcd(x, y) * y

View file

@ -1,4 +0,0 @@
irb(main):004:0> lcm 12, 18
=> 36
irb(main):005:0> lcm 12, 18, 22
=> 396

View file

@ -0,0 +1,16 @@
use std::cmp::{min, max};
fn gcd_stein(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), (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!(),
}
}
fn lcm(a: usize, b: usize) -> usize {
a * b / gcd_stein(a,b)
}

View file

@ -0,0 +1,2 @@
$ txr -p '(lcm (expt 2 123) (expt 6 49) 17)'
43259338018880832376582582128138484281161556655442781051813888

View file

@ -0,0 +1 @@
fcn lcm(m,n){ (m*n).abs()/m.gcd(n) } // gcd is a number method