# HG changeset patch # User Chris Cannam # Date 1382533122 -3600 # Node ID aae8a83a4ebb9b46f27eea95135694ca56c73f6c # Parent 1404b9b588684ff8d754944349c0e4eb012bac91 New module for some simple maths functions diff -r 1404b9b58868 -r aae8a83a4ebb src/may/mathmisc.yeti --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/may/mathmisc.yeti Wed Oct 23 13:58:42 2013 +0100 @@ -0,0 +1,12 @@ + +module may.mathmisc; + +gcd a b = (c = a % b; if c == 0 then b else gcd b c fi); + +factorial x = if x < 0 then 0 else fold do x y: x*y done 1 [1..x] fi; + +{ + gcd, + factorial, +} + diff -r 1404b9b58868 -r aae8a83a4ebb src/may/mathmisc/test/test_mathmisc.yeti --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/may/mathmisc/test/test_mathmisc.yeti Wed Oct 23 13:58:42 2013 +0100 @@ -0,0 +1,33 @@ + +module may.mathmisc.test.test_mathmisc; + +mm = load may.mathmisc; + +{ compare, compareUsing, assert } = load may.test.test; + +[ + +"gcd": \( + compare (mm.gcd 1 1) 1 and + compare (mm.gcd 2 1) 1 and + compare (mm.gcd 2 3) 1 and + compare (mm.gcd 4 2) 2 and + compare (mm.gcd 18 24) 6 and + compare (mm.gcd 27 18) 9 and + compare (mm.gcd 18 36) 18 and + compare (mm.gcd 37 18) 1 +), + +"factorial": \( + compare (mm.factorial (-10)) 0 and + compare (mm.factorial 0) 1 and + compare (mm.factorial 1) 1 and + compare (mm.factorial 2) 2 and + compare (mm.factorial 3) 6 and + compare (mm.factorial 4) 24 and + compare (mm.factorial 4.5) 24 and + compare (mm.factorial 20) 2432902008176640000 +), + +] is hash boolean>; +