From cad3deab3aa527e41014b0e76cb5c622cd20a1e5 Mon Sep 17 00:00:00 2001 From: Torsten Ruger Date: Thu, 5 Nov 2015 13:58:59 +0200 Subject: [PATCH] add working fibonacci examples, while and recursive for fibo(10) the difference is 270 vs 6500 instructions, a lot apparently there are as many calls as the fibs result, i.e. 55, for recursive --- codes/a_fibo_recur.soml | 22 ++++++++++++++++++++++ codes/a_fibo_while.soml | 18 ++++++++++++++++++ 2 files changed, 40 insertions(+) create mode 100644 codes/a_fibo_recur.soml create mode 100644 codes/a_fibo_while.soml diff --git a/codes/a_fibo_recur.soml b/codes/a_fibo_recur.soml new file mode 100644 index 0000000..6432f85 --- /dev/null +++ b/codes/a_fibo_recur.soml @@ -0,0 +1,22 @@ +class Object + int fibonaccir( int n ) + if_plus( n - 1 ) + int tmp + tmp = n - 1 + int a = fibonaccir( tmp ) + tmp = n - 2 + int b = fibonaccir( tmp ) + return a + b + else + return n + end + end + int fib_print(int n) + int fib = fibonaccir( n ) + fib.putint() + return fib + end + int main() + return fib_print(10) + end +end diff --git a/codes/a_fibo_while.soml b/codes/a_fibo_while.soml new file mode 100644 index 0000000..4d0b764 --- /dev/null +++ b/codes/a_fibo_while.soml @@ -0,0 +1,18 @@ +class Object + int fibonaccit(int n) + int a = 0 + int b = 1 + while_plus( n ) + int tmp = a + a = b + b = tmp + b + n = n - 1 + end + b.putint() + return b + end + + int main() + return fibonaccit( 10 ) + end +end