mirror of
https://github.com/Dummi26/mers.git
synced 2025-03-10 05:43:53 +01:00
add GCD example
This commit is contained in:
parent
fd2a3382fc
commit
09fbf2ab0a
30
examples/05_Greatest_Common_Divisor.mers
Normal file
30
examples/05_Greatest_Common_Divisor.mers
Normal file
@ -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
|
Loading…
Reference in New Issue
Block a user