From 09fbf2ab0ae5adc5c4ef9b4bba402269ca7d49cc Mon Sep 17 00:00:00 2001 From: Mark Date: Fri, 27 Oct 2023 13:50:26 +0200 Subject: [PATCH] add GCD example --- examples/05_Greatest_Common_Divisor.mers | 30 ++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 examples/05_Greatest_Common_Divisor.mers diff --git a/examples/05_Greatest_Common_Divisor.mers b/examples/05_Greatest_Common_Divisor.mers new file mode 100644 index 0000000..b65f8d5 --- /dev/null +++ b/examples/05_Greatest_Common_Divisor.mers @@ -0,0 +1,30 @@ +gcd := vals -> { + () -> { + (a, b) := vals + if (a, b).eq + (a) + else if ((a, b).diff.signum, 1).eq + &vals = (a, (a, b).diff) + else + &vals = ((b, a).diff, b) + } +}.loop + +get_num := () -> { + line := ().read_line.trim + ( + line.parse_float, + ( + () -> { + ("error: '", line, "' not a number!").concat.println + 1.panic + } + n -> n, + ) + ).try +} + +("gcd of 899 and 2900 is ", (899, 2900).gcd).concat.println // 29 +"Now type two numbers!".println +(a, b) := (().get_num, ().get_num) +("gcd of ", a, " and ", b, " is ", (a, b).gcd).concat.println