Skip to content

Commit

Permalink
Restructured recursive examples
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisEdel committed Jul 25, 2019
1 parent b3de1ed commit 2c10bf2
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 16 deletions.
12 changes: 6 additions & 6 deletions manuscript/code/symbolic/nested-recursion.c
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
11 changes: 6 additions & 5 deletions manuscript/code/symbolic/recursive-ackermann.c
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
10 changes: 5 additions & 5 deletions manuscript/code/symbolic/recursive-factorial.c
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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;
Expand Down

0 comments on commit 2c10bf2

Please sign in to comment.