Another update from ingydotnet^djgoku

This commit is contained in:
Ingy döt Net 2015-11-18 06:14:39 +00:00
parent 91df62d461
commit 948b86eafa
7604 changed files with 108452 additions and 22726 deletions

View file

@ -0,0 +1,15 @@
BEGIN
PROC gcd = (INT m, n) INT :
BEGIN
INT a := ABS m, b := ABS n;
IF a=0 OR b=0 THEN 0 ELSE
WHILE b /= 0 DO INT t = b; b := a MOD b; a := t OD;
a
FI
END;
PROC lcm = (INT m, n) INT : ( m*n = 0 | 0 | ABS (m*n) % gcd (m, n));
INT m=12, n=18;
printf (($gxg(0)3(xgxg(0))l$,
"The least common multiple of", m, "and", n, "is", lcm(m,n),
"and their greatest common divisor is", gcd(m,n)))
END

View file

@ -0,0 +1,9 @@
begin
integer procedure gcd ( integer value a, b ) ;
if b = 0 then a else gcd( b, a rem abs(b) );
integer procedure lcm( integer value a, b ) ;
abs( a * b ) div gcd( a, b );
write( lcm( 15, 20 ) );
end.

View file

@ -0,0 +1,90 @@
; lcm.asm: calculates the least common multiple
; of two positive integers
;
; nasm x86_64 assembly (linux) with libc
; assemble: nasm -felf64 lcm.asm; gcc lcm.o
; usage: ./a.out [number1] [number2]
global main
extern printf ; c function: prints formatted output
extern strtol ; c function: converts strings to longs
section .text
main:
push rbp ; set up stack frame
; rdi contains argc
; if less than 3, exit
cmp rdi, 3
jl incorrect_usage
; push first argument as number
push rsi
mov rdi, [rsi+8]
mov rsi, 0
mov rdx, 10 ; base 10
call strtol
pop rsi
push rax
; push second argument as number
push rsi
mov rdi, [rsi+16]
mov rsi, 0
mov rdx, 10 ; base 10
call strtol
pop rsi
push rax
; pop arguments and call get_gcd
pop rdi
pop rsi
call get_gcd
; print value
mov rdi, print_number
mov rsi, rax
call printf
; exit
mov rax, 0 ; 0--exit success
pop rbp
ret
incorrect_usage:
mov rdi, bad_use_string
; rsi already contains argv
mov rsi, [rsi]
call printf
mov rax, 0 ; 0--exit success
pop rbp
ret
bad_use_string:
db "Usage: %s [number1] [number2]",10,0
print_number:
db "%d",10,0
get_gcd:
push rbp ; set up stack frame
mov rax, 0
jmp loop
loop:
; keep adding the first argument
; to itself until a multiple
; is found. then, return
add rax, rdi
push rax
mov rdx, 0
div rsi
cmp rdx, 0
pop rax
je gcd_found
jmp loop
gcd_found:
pop rbp
ret

View file

@ -0,0 +1,18 @@
@echo off
setlocal enabledelayedexpansion
set num1=12
set num2=18
call :lcm %num1% %num2%
exit /b
:lcm <input1> <input2>
if %2 equ 0 (
set /a lcm = %num1%*%num2%/%1
echo LCM = !lcm!
pause>nul
goto :EOF
)
set /a res = %1 %% %2
call :lcm %2 %res%
goto :EOF

View file

@ -0,0 +1,26 @@
#include <cstdlib>
#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);
}
return a;
}
int lcm(int a, int b) {
int c = gcd(a, b);
return c == 0 ? 0 : a / c * 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;
return 0;
}

View file

@ -0,0 +1,8 @@
defmodule RC do
def gcd(a,0), do: abs(a)
def gcd(a,b), do: gcd(b, rem(a,b))
def lcm(a,b), do: div(abs(a*b), gcd(a,b))
end
IO.puts RC.lcm(-12,15)

View file

@ -0,0 +1 @@
=LCM(A1:J1)

View file

@ -0,0 +1,14 @@
integer function lcm(a,b)
integer:: a,b
lcm = a*b / gcd(a,b)
end function lcm
integer function gcd(a,b)
integer :: a,b,t
do while (b/=0)
t = b
b = mod(a,b)
a = t
end do
gcd = abs(a)
end function gcd

View file

@ -4,7 +4,7 @@
36 132
*./ 12 18 22
396
0 1 0 1 *. 0 0 1 1 NB. for boolean arguments (0 and 1) it is equivalent to "and"
0 1 0 1 *. 0 0 1 1 NB. for truth valued arguments (0 and 1) it is equivalent to "and"
0 0 0 1
*./~ 0 1
0 0

View file

@ -0,0 +1,16 @@
function gcd ($a, $b) {
function pgcd ($n, $m) {
if($n -le $m) {
if($n -eq 0) {$m}
else{pgcd $n ($m-$n)}
}
else {pgcd $m $n}
}
$n = [Math]::Abs($a)
$m = [Math]::Abs($b)
(pgcd $n $m)
}
function lcm ($a, $b) {
[Math]::Abs($a*$b)/(gcd $a $b)
}
lcm 12 18

View file

@ -0,0 +1,16 @@
function gcd ($a, $b) {
function pgcd ($n, $m) {
if($n -le $m) {
if($n -eq 0) {$m}
else{pgcd $n ($m%$n)}
}
else {pgcd $m $n}
}
$n = [Math]::Abs($a)
$m = [Math]::Abs($b)
(pgcd $n $m)
}
function lcm ($a, $b) {
[Math]::Abs($a*$b)/(gcd $a $b)
}
lcm 12 18

View file

@ -1,4 +1,2 @@
irb(main):001:0> require 'rational'
=> true
irb(main):002:0> 12.lcm 18
irb(main):001:0> 12.lcm 18
=> 36

View file

@ -1,10 +1,14 @@
def gcd(m, n)
m, n = n, m % n until n.zero?
m.abs
end
def lcm(*args)
args.inject(1) do |m, n|
next 0 if m == 0 or n == 0
i = m
loop do
break i if i % n == 0
i += m
end
return 0 if n.zero?
(m * n).abs / gcd(m, n)
end
end
p lcm 12, 18, 22
p lcm 15, 14, -6, 10, 21

View file

@ -1,2 +1,2 @@
lcm(12, 18)
lcm(12,18
36

View file

@ -0,0 +1,30 @@
Function LCM(a,b)
LCM = POS((a * b)/GCD(a,b))
End Function
Function GCD(a,b)
Do
If a Mod b > 0 Then
c = a Mod b
a = b
b = c
Else
GCD = b
Exit Do
End If
Loop
End Function
Function POS(n)
If n < 0 Then
POS = n * -1
Else
POS = n
End If
End Function
i = WScript.Arguments(0)
j = WScript.Arguments(1)
WScript.StdOut.Write "The LCM of " & i & " and " & j & " is " & LCM(i,j) & "."
WScript.StdOut.WriteLine