langs a-z
This commit is contained in:
parent
db842d013d
commit
d066446780
11389 changed files with 98361 additions and 1020 deletions
11
Task/Least-common-multiple/OCaml/least-common-multiple.ocaml
Normal file
11
Task/Least-common-multiple/OCaml/least-common-multiple.ocaml
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
let rec gcd u v =
|
||||
if v <> 0 then (gcd v (u mod v))
|
||||
else (abs u)
|
||||
|
||||
let lcm m n =
|
||||
match m, n with
|
||||
| 0, _ | _, 0 -> 0
|
||||
| m, n -> abs (m * n) / (gcd m n)
|
||||
|
||||
let () =
|
||||
Printf.printf "lcm(35, 21) = %d\n" (lcm 21 35)
|
||||
|
|
@ -0,0 +1,15 @@
|
|||
class LCM {
|
||||
function : Main(args : String[]) ~ Nil {
|
||||
IO.Console->Print("lcm(35, 21) = ")->PrintLine(lcm(21,35));
|
||||
}
|
||||
|
||||
function : lcm(m : Int, n : Int) ~ Int {
|
||||
return m / gcd(m, n) * n;
|
||||
}
|
||||
|
||||
function : gcd(m : Int, n : Int) ~ Int {
|
||||
tmp : Int;
|
||||
while(m <> 0) { tmp := m; m := n % m; n := tmp; };
|
||||
return n;
|
||||
}
|
||||
}
|
||||
14
Task/Least-common-multiple/Order/least-common-multiple.order
Normal file
14
Task/Least-common-multiple/Order/least-common-multiple.order
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
#include <order/interpreter.h>
|
||||
|
||||
#define ORDER_PP_DEF_8gcd ORDER_PP_FN( \
|
||||
8fn(8U, 8V, \
|
||||
8if(8isnt_0(8V), 8gcd(8V, 8remainder(8U, 8V)), 8U)))
|
||||
|
||||
#define ORDER_PP_DEF_8lcm ORDER_PP_FN( \
|
||||
8fn(8X, 8Y, \
|
||||
8if(8or(8is_0(8X), 8is_0(8Y)), \
|
||||
0, \
|
||||
8quotient(8times(8X, 8Y), 8gcd(8X, 8Y)))))
|
||||
// No support for negative numbers
|
||||
|
||||
ORDER_PP( 8to_lit(8lcm(12, 18)) ) // 36
|
||||
|
|
@ -0,0 +1 @@
|
|||
lcm
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
Program LeastCommonMultiple(output);
|
||||
|
||||
function lcm(a, b: longint): longint;
|
||||
begin
|
||||
lcm := a;
|
||||
while (lcm mod b) <> 0 do
|
||||
inc(lcm, a);
|
||||
end;
|
||||
|
||||
begin
|
||||
writeln('The least common multiple of 12 and 18 is: ', lcm(12, 18));
|
||||
end.
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
say 3 lcm 4; # infix
|
||||
say [lcm] 1..20; # reduction
|
||||
say ~(1..10 Xlcm 1..10) # cross
|
||||
|
|
@ -0,0 +1,17 @@
|
|||
Procedure GCDiv(a, b); Euclidean algorithm
|
||||
Protected r
|
||||
While b
|
||||
r = b
|
||||
b = a%b
|
||||
a = r
|
||||
Wend
|
||||
ProcedureReturn a
|
||||
EndProcedure
|
||||
|
||||
Procedure LCM(m,n)
|
||||
Protected t
|
||||
If m And n
|
||||
t=m*n/GCDiv(m,n)
|
||||
EndIf
|
||||
ProcedureReturn t*Sign(t)
|
||||
EndProcedure
|
||||
5
Task/Least-common-multiple/Qi/least-common-multiple.qi
Normal file
5
Task/Least-common-multiple/Qi/least-common-multiple.qi
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
(define gcd
|
||||
A 0 -> A
|
||||
A B -> (gcd B (MOD A B)))
|
||||
|
||||
(define lcm A B -> (/ (* A B) (gcd A B)))
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
: gcd ( ab-n ) [ tuck mod dup ] while drop ;
|
||||
: lcm ( ab-n ) 2over gcd [ * ] dip / ;
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
print lcm(22,44)
|
||||
|
||||
function lcm(m,n)
|
||||
while n
|
||||
t = m
|
||||
m = n
|
||||
n = t mod n
|
||||
wend
|
||||
lcm = m
|
||||
end function
|
||||
23
Task/Least-common-multiple/Seed7/least-common-multiple.seed7
Normal file
23
Task/Least-common-multiple/Seed7/least-common-multiple.seed7
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
$ include "seed7_05.s7i";
|
||||
|
||||
const func integer: gcd (in var integer: a, in var integer: b) is func
|
||||
result
|
||||
var integer: gcd is 0;
|
||||
local
|
||||
var integer: help is 0;
|
||||
begin
|
||||
while a <> 0 do
|
||||
help := b rem a;
|
||||
b := a;
|
||||
a := help;
|
||||
end while;
|
||||
gcd := b;
|
||||
end func;
|
||||
|
||||
const func integer: lcm (in integer: a, in integer: b) is
|
||||
return a div gcd(a, b) * b;
|
||||
|
||||
const proc: main is func
|
||||
begin
|
||||
writeln("lcm(35, 21) = " <& lcm(21, 35));
|
||||
end func;
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
lcm(12, 18)
|
||||
36
|
||||
30
Task/Least-common-multiple/TSE-SAL/least-common-multiple.tse
Normal file
30
Task/Least-common-multiple/TSE-SAL/least-common-multiple.tse
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
// library: math: get: least: common: multiple <description></description> <version control></version control> <version>1.0.0.0.2</version> <version control></version control> (filenamemacro=getmacmu.s) [<Program>] [<Research>] [kn, ri, su, 20-01-2013 14:36:11]
|
||||
INTEGER PROC FNMathGetLeastCommonMultipleI( INTEGER x1I, INTEGER x2I )
|
||||
//
|
||||
RETURN( x1I * x2I / FNMathGetGreatestCommonDivisorI( x1I, x2I ) )
|
||||
//
|
||||
END
|
||||
|
||||
// library: math: get: greatest: common: divisor <description>greatest common divisor whole numbers. Euclid's algorithm. Recursive version</description> <version control></version control> <version>1.0.0.0.3</version> <version control></version control> (filenamemacro=getmacdi.s) [<Program>] [<Research>] [kn, ri, su, 20-01-2013 14:22:41]
|
||||
INTEGER PROC FNMathGetGreatestCommonDivisorI( INTEGER x1I, INTEGER x2I )
|
||||
//
|
||||
IF ( x2I == 0 )
|
||||
//
|
||||
RETURN( x1I )
|
||||
//
|
||||
ENDIF
|
||||
//
|
||||
RETURN( FNMathGetGreatestCommonDivisorI( x2I, x1I MOD x2I ) )
|
||||
//
|
||||
END
|
||||
|
||||
PROC Main()
|
||||
//
|
||||
STRING s1[255] = "10"
|
||||
STRING s2[255] = "20"
|
||||
REPEAT
|
||||
IF ( NOT ( Ask( "math: get: least: common: multiple: x1I = ", s1, _EDIT_HISTORY_ ) ) AND ( Length( s1 ) > 0 ) ) RETURN() ENDIF
|
||||
IF ( NOT ( Ask( "math: get: least: common: multiple: x2I = ", s2, _EDIT_HISTORY_ ) ) AND ( Length( s2 ) > 0 ) ) RETURN() ENDIF
|
||||
Warn( FNMathGetLeastCommonMultipleI( Val( s1 ), Val( s2 ) ) ) // gives e.g. 10
|
||||
UNTIL FALSE
|
||||
END
|
||||
|
|
@ -0,0 +1,21 @@
|
|||
gcd() {
|
||||
# Calculate $1 % $2 until $2 becomes zero.
|
||||
until test 0 -eq "$2"; do
|
||||
# Parallel assignment: set -- 1 2
|
||||
set -- "$2" "`expr "$1" % "$2"`"
|
||||
done
|
||||
|
||||
# Echo absolute value of $1.
|
||||
test 0 -gt "$1" && set -- "`expr 0 - "$1"`"
|
||||
echo "$1"
|
||||
}
|
||||
|
||||
lcm() {
|
||||
set -- "$1" "$2" "`gcd "$1" "$2"`"
|
||||
set -- "`expr "$1" \* "$2" / "$3"`"
|
||||
test 0 -gt "$1" && set -- "`expr 0 - "$1"`"
|
||||
echo "$1"
|
||||
}
|
||||
|
||||
lcm 30 -42
|
||||
# => 210
|
||||
|
|
@ -0,0 +1,24 @@
|
|||
alias gcd eval \''set gcd_args=( \!*:q ) \\
|
||||
@ gcd_u=$gcd_args[2] \\
|
||||
@ gcd_v=$gcd_args[3] \\
|
||||
while ( $gcd_v != 0 ) \\
|
||||
@ gcd_t = $gcd_u % $gcd_v \\
|
||||
@ gcd_u = $gcd_v \\
|
||||
@ gcd_v = $gcd_t \\
|
||||
end \\
|
||||
if ( $gcd_u < 0 ) @ gcd_u = - $gcd_u \\
|
||||
@ $gcd_args[1]=$gcd_u \\
|
||||
'\'
|
||||
|
||||
alias lcm eval \''set lcm_args=( \!*:q ) \\
|
||||
@ lcm_m = $lcm_args[2] \\
|
||||
@ lcm_n = $lcm_args[3] \\
|
||||
gcd lcm_d $lcm_m $lcm_n \\
|
||||
@ lcm_r = ( $lcm_m * $lcm_n ) / $lcm_d \\
|
||||
if ( $lcm_r < 0 ) @ lcm_r = - $lcm_r \\
|
||||
@ $lcm_args[1] = $lcm_r \\
|
||||
'\'
|
||||
|
||||
lcm result 30 -42
|
||||
echo $result
|
||||
# => 210
|
||||
26
Task/Least-common-multiple/Vala/least-common-multiple.vala
Normal file
26
Task/Least-common-multiple/Vala/least-common-multiple.vala
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
int lcm(int a, int b){
|
||||
/*Return least common multiple of two ints*/
|
||||
// check for 0's
|
||||
if (a == 0 || b == 0)
|
||||
return 0;
|
||||
|
||||
// Math.abs(x) only works for doubles, Math.absf(x) for floats
|
||||
if (a < 0)
|
||||
a *= -1;
|
||||
if (b < 0)
|
||||
b *= -1;
|
||||
|
||||
int x = 1;
|
||||
while (true){
|
||||
if (a * x % b == 0)
|
||||
return a*x;
|
||||
x++;
|
||||
}
|
||||
}
|
||||
|
||||
void main(){
|
||||
int a = 12;
|
||||
int b = 18;
|
||||
|
||||
stdout.printf("lcm(%d, %d) = %d\n", a, b, lcm(a, b));
|
||||
}
|
||||
16
Task/Least-common-multiple/XPL0/least-common-multiple.xpl0
Normal file
16
Task/Least-common-multiple/XPL0/least-common-multiple.xpl0
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
include c:\cxpl\codes;
|
||||
|
||||
func GCD(M,N); \Return the greatest common divisor of M and N
|
||||
int M, N;
|
||||
int T;
|
||||
[while N do \Euclid's method
|
||||
[T:= M; M:= N; N:= rem(T/N)];
|
||||
return M;
|
||||
];
|
||||
|
||||
func LCM(M,N); \Return least common multiple
|
||||
int M, N;
|
||||
return abs(M*N) / GCD(M,N);
|
||||
|
||||
\Display the LCM of two integers entered on command line
|
||||
IntOut(0, LCM(IntIn(8), IntIn(8)))
|
||||
Loading…
Add table
Add a link
Reference in a new issue