Time for an 2014 update…
This commit is contained in:
parent
372c577f83
commit
09687c4926
2520 changed files with 34227 additions and 7318 deletions
|
|
@ -0,0 +1,13 @@
|
|||
Func _LCM($a, $b)
|
||||
Local $c, $f, $m = $a, $n = $b
|
||||
$c = 1
|
||||
While $c <> 0
|
||||
$f = Int($a / $b)
|
||||
$c = $a - $b * $f
|
||||
If $c <> 0 Then
|
||||
$a = $b
|
||||
$b = $c
|
||||
EndIf
|
||||
WEnd
|
||||
Return $m * $n / $b
|
||||
EndFunc ;==>_LCM
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
ConsoleWrite(_LCM(12,18) & @LF)
|
||||
ConsoleWrite(_LCM(-5,12) & @LF)
|
||||
ConsoleWrite(_LCM(13,0) & @LF)
|
||||
|
|
@ -0,0 +1,22 @@
|
|||
10 DEF FN MOD(A) = INT((A / B - INT(A / B)) * B + .05) * SGN(A / B)
|
||||
20 INPUT"M=";M%
|
||||
30 INPUT"N=";N%
|
||||
40 GOSUB 100
|
||||
50 PRINT R
|
||||
60 END
|
||||
|
||||
100 REM LEAST COMMON MULTIPLE M% N%
|
||||
110 R = 0
|
||||
120 IF M% = 0 OR N% = 0 THEN RETURN
|
||||
130 A% = M% : B% = N% : GOSUB 200"GCD
|
||||
140 R = ABS(M%*N%)/R
|
||||
150 RETURN
|
||||
|
||||
200 REM GCD ITERATIVE EUCLID A% B%
|
||||
210 FOR B = B% TO 0 STEP 0
|
||||
220 C% = A%
|
||||
230 A% = B
|
||||
240 B = FN MOD(C%)
|
||||
250 NEXT B
|
||||
260 R = ABS(A%)
|
||||
270 RETURN
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
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%)
|
||||
|
|
@ -1,24 +1,22 @@
|
|||
import std.stdio, std.bigint;
|
||||
import std.stdio, std.bigint, std.math;
|
||||
|
||||
T lcm(T)(/*in*/ T m, /*in*/ T n) /*pure nothrow*/ {
|
||||
if (m == 0) return m;
|
||||
if (n == 0) return n;
|
||||
T r = (m * n) / gcd(m, n);
|
||||
//return abs(r);
|
||||
return (r < 0) ? -r : r;
|
||||
}
|
||||
|
||||
T gcd(T)(/*in*/ T a, /*in*/ T b) /*pure nothrow*/ {
|
||||
while (b != 0) {
|
||||
auto t = b;
|
||||
T gcd(T)(T a, T b) pure /*nothrow*/ {
|
||||
while (b) {
|
||||
immutable t = b;
|
||||
b = a % b;
|
||||
a = t;
|
||||
}
|
||||
return a;
|
||||
}
|
||||
|
||||
void main() {
|
||||
writeln(lcm(12, 18));
|
||||
writeln(lcm(BigInt("2562047788015215500854906332309589561"),
|
||||
BigInt("6795454494268282920431565661684282819")));
|
||||
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));
|
||||
}
|
||||
|
||||
void main() {
|
||||
lcm(12, 18).writeln;
|
||||
lcm("2562047788015215500854906332309589561".BigInt,
|
||||
"6795454494268282920431565661684282819".BigInt).writeln;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,14 @@
|
|||
function LCM(A) // A is an integer array (e.g. [-50,25,-45,-18,90,447])
|
||||
{
|
||||
var n = A.length, a = Math.abs(A[0]);
|
||||
for (var i = 1; i < n; i++)
|
||||
{ var b = Math.abs(A[i]), c = a;
|
||||
while (a && b){ a > b ? a %= b : b %= a; }
|
||||
a = Math.abs(c*A[i])/(a+b);
|
||||
}
|
||||
return a;
|
||||
}
|
||||
|
||||
/* For example:
|
||||
LCM([-50,25,-45,-18,90,447]) -> 67050
|
||||
*/
|
||||
|
|
@ -0,0 +1 @@
|
|||
lcm(m,n)
|
||||
|
|
@ -0,0 +1,70 @@
|
|||
/* NetRexx */
|
||||
options replace format comments java crossref symbols nobinary
|
||||
|
||||
numeric digits 3000
|
||||
|
||||
runSample(arg)
|
||||
return
|
||||
|
||||
-- ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
|
||||
method lcm(m_, n_) public static
|
||||
L_ = m_ * n_ % gcd(m_, n_)
|
||||
return L_
|
||||
|
||||
-- ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
|
||||
-- Euclid's algorithm - iterative implementation
|
||||
method gcd(m_, n_) public static
|
||||
loop while n_ > 0
|
||||
c_ = m_ // n_
|
||||
m_ = n_
|
||||
n_ = c_
|
||||
end
|
||||
return m_
|
||||
|
||||
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
method runSample(arg) private static
|
||||
parse arg samples
|
||||
if samples = '' | samples = '.' then
|
||||
samples = '-6 14 = 42 |' -
|
||||
'3 4 = 12 |' -
|
||||
'18 12 = 36 |' -
|
||||
'2 0 = 0 |' -
|
||||
'0 85 = 0 |' -
|
||||
'12 18 = 36 |' -
|
||||
'5 12 = 60 |' -
|
||||
'12 22 = 132 |' -
|
||||
'7 31 = 217 |' -
|
||||
'117 18 = 234 |' -
|
||||
'38 46 = 874 |' -
|
||||
'18 12 -5 = 180 |' -
|
||||
'-5 18 12 = 180 |' - -- confirm that other permutations work
|
||||
'12 -5 18 = 180 |' -
|
||||
'18 12 -5 97 = 17460 |' -
|
||||
'30 42 = 210 |' -
|
||||
'30 42 = . |' - -- 210; no verification requested
|
||||
'18 12' -- 36
|
||||
|
||||
loop while samples \= ''
|
||||
parse samples sample '|' samples
|
||||
loop while sample \= ''
|
||||
parse sample mnvals '=' chk sample
|
||||
if chk = '' then chk = '.'
|
||||
mv = mnvals.word(1)
|
||||
loop w_ = 2 to mnvals.words mnvals
|
||||
nv = mnvals.word(w_)
|
||||
mv = mv.abs
|
||||
nv = nv.abs
|
||||
mv = lcm(mv, nv)
|
||||
end w_
|
||||
lv = mv
|
||||
select case chk
|
||||
when '.' then state = ''
|
||||
when lv then state = '(verified)'
|
||||
otherwise state = '(failed)'
|
||||
end
|
||||
mnvals = mnvals.space(1, ',').changestr(',', ', ')
|
||||
say 'lcm of' mnvals.right(15.max(mnvals.length)) 'is' lv.right(5.max(lv.length)) state
|
||||
end
|
||||
end
|
||||
|
||||
return
|
||||
Loading…
Add table
Add a link
Reference in a new issue