Skip to content

Euclidean algorithm: Output standardization #877

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 32 commits into from
Jan 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
314a085
Standardize bash output
stormofice Oct 12, 2021
abbd81a
Standardize c output
stormofice Oct 12, 2021
195fe6f
Standardize c++ output
stormofice Oct 12, 2021
bad949d
Standardize lisp output
stormofice Oct 12, 2021
719cd55
Standardize c# output
stormofice Oct 12, 2021
1a2f753
Standardize d output
stormofice Oct 12, 2021
0c640e3
Standardize fortran output
stormofice Oct 12, 2021
f6b4c16
Standardize go output
stormofice Oct 12, 2021
e525f06
Standardize haskell output
stormofice Oct 12, 2021
b443f66
Standardize java output
stormofice Oct 12, 2021
27afabf
Standardize javascript output
stormofice Oct 12, 2021
328e8a4
Standardize kotlin output
stormofice Oct 12, 2021
9f95a61
Standardize lua output
stormofice Oct 12, 2021
511b446
Standardize matlab output
stormofice Oct 12, 2021
42fd666
Standardize nim output
stormofice Oct 12, 2021
3095ea8
Standardize powershell output
stormofice Oct 12, 2021
7edccc7
Standardize rust output
stormofice Oct 12, 2021
cd1def4
Standardize scala output.
stormofice Oct 12, 2021
f5f95fb
Standardize php output
stormofice Oct 12, 2021
18dc8ad
Standardize swift output
stormofice Oct 12, 2021
7b18665
Fix kotlin printing order
stormofice Oct 12, 2021
1f77cc2
Use python3 printing style
stormofice Oct 12, 2021
84210b0
Merge master
stormofice Nov 6, 2021
c0d00fa
Revert "Merge master"
stormofice Nov 6, 2021
0d87122
Standardize OCaml output
stormofice Dec 1, 2021
0de4c6c
Standardize asm-x64 output
stormofice Dec 1, 2021
ab4f8c2
Standardize coconut output
stormofice Dec 1, 2021
19033a9
Standardize racket output
stormofice Dec 1, 2021
a7e318d
Standardize scheme output
stormofice Dec 1, 2021
90b6858
Update contents/euclidean_algorithm/code/fortran/euclidean.f90
stormofice Dec 2, 2021
fe5f734
Merge branch 'main' into euclidean-io-overhaul
stormofice Dec 2, 2021
dec2dbf
Merge branch 'main' into euclidean-io-overhaul
Amaras Jan 13, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions contents/euclidean_algorithm/code/asm-x64/euclidean_example.s
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
.intel_syntax noprefix

.section .rodata
fmt: .string "%d\n"
euclid_mod_fmt: .string "[#]\nModulus-based euclidean algorithm result:\n%d\n"
euclid_sub_fmt: .string "[#]\nSubtraction-based euclidean algorithm result:\n%d\n"

.section .text
.global main
Expand Down Expand Up @@ -59,14 +60,14 @@ main:
mov rdi, 4288 # Call euclid_mod
mov rsi, 5184
call euclid_mod
mov rdi, OFFSET fmt # Print output
mov rdi, OFFSET euclid_mod_fmt # Print output
mov rsi, rax
xor rax, rax
call printf
mov rdi, 1536 # Call euclid_sub
mov rsi, 9856
call euclid_sub
mov rdi, OFFSET fmt # Print output
mov rdi, OFFSET euclid_sub_fmt # Print output
mov rsi, rax
xor rax, rax
call printf
Expand Down
4 changes: 2 additions & 2 deletions contents/euclidean_algorithm/code/bash/euclid.bash
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,6 @@ euclid_sub() {
}

result=$(euclid_mod $((64 * 67)) $((64 * 81)))
echo "$result"
echo -e "[#]\nModulus-based euclidean algorithm result:\n$result"
result=$(euclid_sub $((128 * 12)) $((128 * 77)))
echo "$result"
echo -e "[#]\nSubtraction-based euclidean algorithm result:\n$result"
4 changes: 2 additions & 2 deletions contents/euclidean_algorithm/code/c/euclidean_example.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ int main() {
int check1 = euclid_mod(64 * 67, 64 * 81);
int check2 = euclid_sub(128 * 12, 128 * 77);

printf("%d\n", check1);
printf("%d\n", check2);
printf("[#]\nModulus-based euclidean algorithm result:\n%d\n", check1);
printf("[#]\nSubtraction-based euclidean algorithm result:\n%d\n", check2);

return 0;
}
6 changes: 4 additions & 2 deletions contents/euclidean_algorithm/code/clisp/euclidean.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@
(abs a)
(euclid-mod b (mod a b))))

(print (euclid-sub (* 64 67) (* 64 81)))
(print (euclid-mod (* 128 12) (* 128 77)))
(format T "[#]~%Modulus-based euclidean algorithm result:~%")
(format T "~d~%" (euclid-sub (* 64 67) (* 64 81)))
(format T "[#]~%Subtraction-based euclidean algorithm result:~%")
(format T "~d~%" (euclid-mod (* 128 12) (* 128 77)))

;; Quick test
(assert
Expand Down
6 changes: 4 additions & 2 deletions contents/euclidean_algorithm/code/coconut/euclidean.coco
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,7 @@ addpattern def euclid_mod(0, b is int) = b
addpattern def euclid_mod(a is int, b is int) = euclid_mod(b, a % b)

if __name__ == '__main__':
print('Euclidean mod:', euclid_mod(64 * 67, 64 * 81))
print('Euclidean sub:', euclid_sub(128 * 12, 128 * 77))
print('[#]\nModulus-based euclidean algorithm result:')
print(euclid_mod(64 * 67, 64 * 81))
print('[#]\nSubtraction-based euclidean algorithm result:')
print(euclid_sub(128 * 12, 128 * 77))
4 changes: 2 additions & 2 deletions contents/euclidean_algorithm/code/cpp/euclidean.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,6 @@ int main() {
auto check1 = euclid_mod(64 * 67, 64 * 81);
auto check2 = euclid_sub(128 * 12, 128 * 77);

std::cout << check1 << '\n';
std::cout << check2 << '\n';
std::cout << "[#]\nModulus-based euclidean algorithm result:\n" << check1 << '\n';
std::cout << "[#]\nSubtraction-based euclidean algorithm result:\n" << check2 << '\n';
}
3 changes: 2 additions & 1 deletion contents/euclidean_algorithm/code/csharp/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ class Program
{
static void Main(string[] args)
{
Console.WriteLine("EuclideanAlgorithm");
var euclideanAlgorithm = new EuclideanAlgorithm();
int check = euclideanAlgorithm.EuclidMod(64 * 67, 64 * 81);
int check2 = euclideanAlgorithm.EuclidSub(128 * 12, 128 * 77);

Console.WriteLine("[#]\nModulus-based euclidean algorithm result:");
Console.WriteLine(check);
Console.WriteLine("[#]\nSubtraction-based euclidean algorithm result:");
Console.WriteLine(check2);
}
}
Expand Down
4 changes: 2 additions & 2 deletions contents/euclidean_algorithm/code/d/euclidean_algorithm.d
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,6 @@ void main()
auto check1 = euclid_mod(64 * 67, 64 * 81);
auto check2 = euclid_sub(128 * 12, 128 * 77);

writeln("Modulus-based euclidean algorithm result: ", check1);
writeln("Subtraction-based euclidean algorithm result: ", check2);
writeln("[#]\nModulus-based euclidean algorithm result:\n", check1);
writeln("[#]\nSubtraction-based euclidean algorithm result:\n", check2);
}
8 changes: 8 additions & 0 deletions contents/euclidean_algorithm/code/expected.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"Description": "euclidean algorithm",
"Delta" : 0.0,
"OutputValues" : [
"64",
"128"
]
}
18 changes: 12 additions & 6 deletions contents/euclidean_algorithm/code/fortran/euclidean.f90
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,18 @@ PROGRAM euclidean
IMPLICIT NONE
INTEGER :: a, b, euclid_sub, euclid_mod

a = 24
b = 27
WRITE(*,*) 'Subtraction method: GCD is: ', euclid_sub(a, b)
a = 64 * 67
b = 64 * 81

a = 24
b = 27
WRITE(*,*) 'Modulus method: GCD is: ', euclid_mod(a, b)
WRITE(*,'(a)') '[#]'
WRITE(*,'(a)') 'Modulus-based euclidean algorithm result:'
WRITE(*, '(g0)') euclid_mod(a, b)

a = 128 * 12
b = 128 * 77

WRITE(*,'(a)') '[#]'
WRITE(*,'(a)') 'Subtraction-based euclidean algorithm result:'
WRITE(*, '(g0)') euclid_sub(a, b)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did we just not run the fortran code before?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We ran the two methods the other way around


END PROGRAM euclidean
2 changes: 2 additions & 0 deletions contents/euclidean_algorithm/code/go/euclidean.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ func main() {
check1 := euclidMod(64*67, 64*81)
check2 := euclidSub(128*12, 128*77)

fmt.Println("[#]\nModulus-based euclidean algorithm result:")
fmt.Println(check1)
fmt.Println("[#]\nSubtraction-based euclidean algorithm result:")
fmt.Println(check2)
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,7 @@ main :: IO ()
main = do
let chk1 = euclidMod (64 * 67) (64 * 81)
chk2 = euclidSub (128 * 12) (128 * 77)
putStrLn "[#]\nModulus-based euclidean algorithm result:"
print chk1
putStrLn "[#]\nSubtraction-based euclidean algorithm result:"
print chk2
2 changes: 2 additions & 0 deletions contents/euclidean_algorithm/code/java/EuclideanAlgo.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ public static int euclidMod(int a, int b) {
}

public static void main(String[] args) {
System.out.println("[#]\nModulus-based euclidean algorithm result:");
System.out.println(euclidMod(64 * 67, 64 * 81));
System.out.println("[#]\nSubtraction-based euclidean algorithm result:");
System.out.println(euclidSub(128 * 12, 128 * 77));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,16 @@ function euclidSub(a, b) {

while (a !== b) {
if (a > b) {
a -= a - b;
a -= b;
} else {
b = b - a;
b -= a;
}
}

return a;
}

console.log('[#]\nModulus-based euclidean algorithm result:')
console.log(euclidMod(64 * 67, 64 * 81));
console.log('[#]\nSubtraction-based euclidean algorithm result:')
console.log(euclidSub(128 * 12, 128 * 77));
4 changes: 2 additions & 2 deletions contents/euclidean_algorithm/code/julia/euclidean.jl
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ function main()
check1 = euclid_mod(64 * 67, 64 * 81);
check2 = euclid_sub(128 * 12, 128 * 77);

println("Modulus-based euclidean algorithm result: $(check1)")
println("subtraction-based euclidean algorithm result: $(check2)")
println("[#]\nModulus-based euclidean algorithm result:\n$(check1)")
println("[#]\nSubtraction-based euclidean algorithm result:\n$(check2)")

end

Expand Down
6 changes: 4 additions & 2 deletions contents/euclidean_algorithm/code/kotlin/Euclidean.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ fun euclidMod(a: Int, b: Int): Int {
}

fun main(args: Array<String>) {
println(euclidSub(128 * 12, 128 * 77))
println("[#]\nModulus-based euclidean algorithm result:")
println(euclidMod(64 * 67, 64 * 81))
}
println("[#]\nSubtraction-based euclidean algorithm result:")
println(euclidSub(128 * 12, 128 * 77))
}
4 changes: 3 additions & 1 deletion contents/euclidean_algorithm/code/lua/euclidean.lua
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@ local function euclid_mod(a, b)
end

local function main()
print(euclid_sub(128 * 12, 128 * 77))
print("[#]\nModulus-based euclidean algorithm result:")
print(euclid_mod(64 * 67, 64 * 81))
print("[#]\nSubtraction-based euclidean algorithm result:")
print(euclid_sub(128 * 12, 128 * 77))
end

main()
5 changes: 3 additions & 2 deletions contents/euclidean_algorithm/code/matlab/euclidean.m
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
end

function euclid()
['gcd(520,420) via euclidSub: ',num2str(euclidSub(520,420))]
['gcd(183,244) via euclidMod: ',num2str(euclidMod(183,244))]
['[#] Modulus-based euclidean algorithm result: ',num2str(euclidMod(64 * 67, 64 * 81))]

['[#] Subtraction-based euclidean algorithm result: ',num2str(euclidSub(128 * 12, 128 * 77))]
end
2 changes: 2 additions & 0 deletions contents/euclidean_algorithm/code/nim/euclid_algorithm.nim
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,7 @@ func euclid_sub(in1, in2: int): int =
result = a

when isMainModule:
echo "[#]\nModulus-based euclidean algorithm result:"
echo euclid_sub(64 * 67, 64 * 81)
echo "[#]\nSubtraction-based euclidean algorithm result:"
echo euclid_mod(128 * 12, 128 * 77)
5 changes: 3 additions & 2 deletions contents/euclidean_algorithm/code/ocaml/euclidean_example.ml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ let euclid_sub a b =
let chk1 = euclid_mod (64 * 67) (64 * 81)
let chk2 = euclid_sub (128 * 12) (128 * 77)
let () =
Printf.printf "[#]\nModulus-based euclidean algorithm result:\n";
chk1 |> print_int |> print_newline;
chk2 |> print_int |> print_newline

Printf.printf "[#]\nSubtraction-based euclidean algorithm result:\n";
chk2 |> print_int |> print_newline
4 changes: 2 additions & 2 deletions contents/euclidean_algorithm/code/php/euclidean.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ function euclid_mod(int $a, int $b): int
return $a;
}

printf('Euclidean mod: %s', euclid_mod(64 * 67, 64 * 81));
printf('[#]'.PHP_EOL.'Modulus-based euclidean algorithm result:'.PHP_EOL.'%s', euclid_mod(64 * 67, 64 * 81));
echo PHP_EOL;
printf('Euclidean sub: %s', euclid_sub(128 * 12, 128 * 77));
printf('[#]'.PHP_EOL.'Subtraction-based euclidean algorithm result:'.PHP_EOL.'%s', euclid_sub(128 * 12, 128 * 77));
echo PHP_EOL;
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,5 @@ function Mod-Euclid($a, $b) {
return $a
}

Write-Host "Subtraction-based euclidean algorithm result: $(Mod-Euclid $(64 * 67) $(64 * 81))"
Write-Host "Modulus-based euclidean algorithm result: $(Sub-Euclid $(128 * 12) $(128 * 77))"
Write-Host "[#]`nSubtraction-based euclidean algorithm result:`n$(Mod-Euclid $(64 * 67) $(64 * 81))"
Write-Host "[#]`nModulus-based euclidean algorithm result:`n$(Sub-Euclid $(128 * 12) $(128 * 77))"
6 changes: 4 additions & 2 deletions contents/euclidean_algorithm/code/python/euclidean_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,7 @@ def euclid_sub(a, b):
return a

if __name__=="__main__":
print('Euclidean mod: ', euclid_mod(64 * 67, 64 * 81))
print('Euclidean sub: ', euclid_sub(128 * 12, 128 * 77))
print('[#]\nModulus-based euclidean algorithm result:'),
print(euclid_mod(64 * 67, 64 * 81))
print('[#]\nSubtraction-based euclidean algorithm result:')
print(euclid_sub(128 * 12, 128 * 77))
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,7 @@
)
)

(displayln "[#]\nModulus-based euclidean algorithm result:")
(displayln (euclid_sub (* 64 67) (* 64 81)))
(displayln "[#]\nSubtraction-based euclidean algorithm result:")
(displayln (euclid_mod (* 128 12) (* 128 77)))
9 changes: 4 additions & 5 deletions contents/euclidean_algorithm/code/ruby/euclidean.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,8 @@ def gcd_minus(a, b)
end
a
end

p gcd_mod(12 * 6, 12 * 4) #=> 12
p gcd_mod(9 * 667, 9 * 104) #=> 9

p gcd_minus(12 * 6, 12 * 4) #=> 12
p gcd_minus(9 * 667, 9 * 104) #=> 9
print "[#]\nModulus-based euclidean algorithm result:\n"
p gcd_mod(64 * 67, 64 * 81)
print "[#]\nSubtraction-based euclidean algorithm result:\n"
p gcd_minus(128 * 12, 128 * 77)
4 changes: 2 additions & 2 deletions contents/euclidean_algorithm/code/rust/euclidean_example.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,6 @@ fn euclid_rem(mut a: i64, mut b: i64) -> i64 {
fn main() {
let chk1 = euclid_rem(64 * 67, 64 * 81);
let chk2 = euclid_sub(128 * 12, 128 * 77);
println!("{}", chk1);
println!("{}", chk2);
println!("[#]\nModulus-based euclidean algorithm result:\n{}", chk1);
println!("[#]\nSubtraction-based euclidean algorithm result:\n{}", chk2);
}
10 changes: 6 additions & 4 deletions contents/euclidean_algorithm/code/scala/euclidean.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ object Euclid {
def euclid_sub(a: Int, b: Int): Int =
(Math.abs(a), Math.abs(b)) match {
case (0, _) | (_, 0) => 0
case (x, y) if x < y => euclid(x, y - x)
case (x, y) if x > y => euclid(x - y, y)
case (x, y) if x < y => euclid_sub(x, y - x)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wait, how did this code run before?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It didn't.
The scala and javascript implementation needed some (small) fixes to work.

case (x, y) if x > y => euclid_sub(x - y, y)
case _ => a
}

Expand All @@ -15,8 +15,10 @@ object Euclid {
}

def main(args: Array[String]): Unit = {
println(euclid_sub(151 * 899, 151 * 182))
println(euclid_mod(151 * 899, 151 * 182))
println("[#]\nModulus-based euclidean algorithm result:")
println(euclid_mod(64 * 67, 64 * 81))
println("[#]\nSubtraction-based euclidean algorithm result:")
println(euclid_sub(128 * 12, 128 * 77))
}

}
4 changes: 3 additions & 1 deletion contents/euclidean_algorithm/code/scheme/euclidalg.ss
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
a
(euclid-mod b (modulo a b))))

(display "[#]\nModulus-based euclidean algorithm result:") (newline)
(display (euclid-mod (* 64 67) (* 64 81))) (newline)
(display (euclid-sub (* 128 12) (* 128 77))) (newline)

(display "[#]\nSubtraction-based euclidean algorithm result:") (newline)
(display (euclid-sub (* 128 12) (* 128 77))) (newline)
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ func euclidMod(a: Int, b: Int) -> Int {
}

func main() {
print("[#]\nModulus-based euclidean algorithm result:")
print(euclidMod(a: 64 * 67, b: 64 * 81))
print("[#]\nSubtraction-based euclidean algorithm result:")
print(euclidSub(a: 128 * 12, b: 128 * 77))
}

Expand Down