summaryrefslogtreecommitdiff
path: root/appl/math/ack.b
diff options
context:
space:
mode:
authorCharles.Forsyth <devnull@localhost>2006-12-22 17:07:39 +0000
committerCharles.Forsyth <devnull@localhost>2006-12-22 17:07:39 +0000
commit37da2899f40661e3e9631e497da8dc59b971cbd0 (patch)
treecbc6d4680e347d906f5fa7fca73214418741df72 /appl/math/ack.b
parent54bc8ff236ac10b3eaa928fd6bcfc0cdb2ba46ae (diff)
20060303a
Diffstat (limited to 'appl/math/ack.b')
-rw-r--r--appl/math/ack.b43
1 files changed, 43 insertions, 0 deletions
diff --git a/appl/math/ack.b b/appl/math/ack.b
new file mode 100644
index 00000000..cbed6fdd
--- /dev/null
+++ b/appl/math/ack.b
@@ -0,0 +1,43 @@
+implement Ackermann;
+
+include "sys.m";
+ sys: Sys;
+include "draw.m";
+ draw: Draw;
+
+Ackermann: module
+{
+ init: fn(ctxt: ref Draw->Context, argv: list of string);
+};
+
+init(nil: ref Draw->Context, argv: list of string)
+{
+ sys = load Sys Sys->PATH;
+ draw = load Draw Draw->PATH;
+ argv = tl argv; # remove program name
+ m := n := 0;
+ if(argv != nil){
+ m = int hd argv;
+ argv = tl argv;
+ }
+ if(m < 0)
+ m = 0;
+ if(argv != nil)
+ n = int hd argv;
+ if(n < 0)
+ n = 0;
+ t0 := sys->millisec();
+ a := ack(m, n);
+ t1 := sys->millisec();
+ sys->print("A(%d, %d) = %d (t = %d ms)\n", m, n, a, t1-t0);
+}
+
+ack(m, n: int) : int
+{
+ if(m == 0)
+ return n+1;
+ else if(n == 0)
+ return ack(m-1, 1);
+ else
+ return ack(m-1, ack(m, n-1));
+}