Just another update

This commit is contained in:
Ingy döt Net 2015-02-20 00:35:01 -05:00
parent a25938f123
commit 00a190b0a6
6591 changed files with 94363 additions and 23227 deletions

View file

@ -7,3 +7,5 @@
(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

@ -1,6 +1,6 @@
import std.stdio, std.bigint, std.math;
T gcd(T)(T a, T b) pure /*nothrow*/ {
T gcd(T)(T a, T b) pure nothrow {
while (b) {
immutable t = b;
b = a % b;
@ -9,7 +9,7 @@ T gcd(T)(T a, T b) pure /*nothrow*/ {
return a;
}
T lcm(T)(T m, T n) pure /*nothrow*/ {
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));

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

View file

@ -0,0 +1,21 @@
lcm2: procedure
x=abs(arg(1))
do k=2 to arg() While x<>0
y=abs(arg(k))
x=x*y/gcd2(x,y)
end
return x
gcd2: procedure
x=abs(arg(1))
do j=2 to arg()
y=abs(arg(j))
If y<>0 Then Do
do until z==0
z=x//y
x=y
y=z
end
end
end
return x

View file

@ -0,0 +1,72 @@
Parse Version v
Say 'Version='v
Call time 'R'
Do a=0 To 100
Do b=0 To 100
Do c=0 To 100
x1.a.b.c=lcm1(a,b,c)
End
End
End
Say 'version 1' time('E')
Call time 'R'
Do a=0 To 100
Do b=0 To 100
Do c=0 To 100
x2.a.b.c=lcm2(a,b,c)
End
End
End
Say 'version 2' time('E')
cnt.=0
Do a=0 To 100
Do b=0 To 100
Do c=0 To 100
If x1.a.b.c=x2.a.b.c then cnt.0ok=cnt.0ok+1
End
End
End
Say cnt.0ok 'comparisons ok'
Exit
/*----------------------------------LCM subroutine----------------------*/
lcm1: procedure; d=strip(arg(1) arg(2)); do i=3 to arg(); d=d arg(i); end
parse var d x d /*obtain the first value in args.*/
x=abs(x) /*use the absolute value of X. */
do while d\=='' /*process the rest of the args. */
parse var d ! d; !=abs(!) /*pick off the next arg (ABS val)*/
if !==0 then return 0 /*if zero, then LCM is also zero.*/
x=x*!/gcd1(x,!) /*have GCD do the heavy lifting.*/
end /*while*/
return x /*return with LCM of arguments.*/
/*----------------------------------GCD subroutine----------------------*/
gcd1: procedure; d=strip(arg(1) arg(2)); do j=3 to arg(); d=d arg(j); end
parse var d x d /*obtain the first value in args.*/
x=abs(x) /*use the absolute value of X. */
do while d\=='' /*process the rest of the args. */
parse var d y d; y=abs(y) /*pick off the next arg (ABS val)*/
if y==0 then iterate /*if zero, then ignore the value.*/
do until y==0; parse value x//y y with y x; end
end /*while*/
return x /*return with GCD of arguments.*/
lcm2: procedure
x=abs(arg(1))
do k=2 to arg() While x<>0
y=abs(arg(k))
x=x*y/gcd2(x,y)
end
return x
gcd2: procedure
x=abs(arg(1))
do j=2 to arg()
y=abs(arg(j))
If y<>0 Then Do
do until z==0
z=x//y
x=y
y=z
end
end
end
return x

View file

@ -1,27 +0,0 @@
/*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. */

View file

@ -0,0 +1 @@
12 lcm: 18