From 2c10bf22891a20f3756013490bc15524fc266eeb Mon Sep 17 00:00:00 2001 From: ChrisEdel Date: Thu, 25 Jul 2019 13:14:18 +0200 Subject: [PATCH] Restructured recursive examples --- manuscript/code/symbolic/nested-recursion.c | 12 ++++++------ manuscript/code/symbolic/recursive-ackermann.c | 11 ++++++----- manuscript/code/symbolic/recursive-factorial.c | 10 +++++----- 3 files changed, 17 insertions(+), 16 deletions(-) diff --git a/manuscript/code/symbolic/nested-recursion.c b/manuscript/code/symbolic/nested-recursion.c index 73c871e8..4c308c67 100644 --- a/manuscript/code/symbolic/nested-recursion.c +++ b/manuscript/code/symbolic/nested-recursion.c @@ -9,17 +9,17 @@ Input == #b00110001 (== 49 == '1') */ uint64_t factorial_upwards(uint64_t n) { - if (n < 10) - return n * factorial_upwards(n + 1); - else + if (n >= 10) return n; + else + return n * factorial_upwards(n + 1); } uint64_t modified_factorial(uint64_t n) { - if (n <= 1) - return factorial_upwards(1); - else + if (n > 1) return n * modified_factorial(n - 1); + else + return factorial_upwards(1); } uint64_t main() { diff --git a/manuscript/code/symbolic/recursive-ackermann.c b/manuscript/code/symbolic/recursive-ackermann.c index 3a413fdc..1fecc2ce 100644 --- a/manuscript/code/symbolic/recursive-ackermann.c +++ b/manuscript/code/symbolic/recursive-ackermann.c @@ -9,12 +9,13 @@ Input == #b00110001 (== 49 == '1') */ uint64_t ackermann(uint64_t m, uint64_t n) { - if (m == 0) + if (m != 0) { + if (n != 0) + return ackermann(m - 1, ackermann(m, n - 1)); + else + return ackermann(m - 1, 1); + } else return n + 1; - else if (n == 0) - return ackermann(m - 1, 1); - else - return ackermann(m - 1, ackermann(m, n - 1)); } uint64_t main() { diff --git a/manuscript/code/symbolic/recursive-factorial.c b/manuscript/code/symbolic/recursive-factorial.c index c6dabbef..d2f089ef 100644 --- a/manuscript/code/symbolic/recursive-factorial.c +++ b/manuscript/code/symbolic/recursive-factorial.c @@ -9,10 +9,10 @@ Input == #b00110001 (== 49 == '1') */ uint64_t factorial(uint64_t n) { - if (n <= 1) - return n; - else + if (n > 1) return n * factorial(n - 1); + else + return n; } uint64_t main() { @@ -23,11 +23,11 @@ uint64_t main() { read(1, x, 1); - *x = *x - 35; + *x = *x - 39; a = factorial(*x); - if (a == 87178291200) + if (a == 3628800) return 1; else return 0;