From 9f3d527820f21cca5b71376724c5b4daef4b4122 Mon Sep 17 00:00:00 2001 From: Ahmad El-Melegy Date: Wed, 28 Jan 2015 19:05:48 +0200 Subject: [PATCH 1/3] Add 1.10 exercise solution --- chapter-1/exercises/1.10.js | 132 ++++++++++++++++++++++++++++++++++++ 1 file changed, 132 insertions(+) diff --git a/chapter-1/exercises/1.10.js b/chapter-1/exercises/1.10.js index e69de29..d06c59d 100644 --- a/chapter-1/exercises/1.10.js +++ b/chapter-1/exercises/1.10.js @@ -0,0 +1,132 @@ +//Exercise 1.10. The following function computes a mathematical function called Ackermann’s function. + +function A(x,y) { + if (y === 0) + return 0; + else if (x === 0) + return 2 * y; + else if (y === 1) + return 2; + else return A(x - 1, A(x, y - 1)); +} + +/* What are the values of the following expressions? +A(1, 10) +A(2, 4) +A(3, 3) +*/ + +//Solution + +/* +A(1, 10) +A(0, A(1, 9)) +A(0, A(0, A(1, 8))) +A(0, A(0, A(0, A(1, 7)))) +A(0, A(0, A(0, A(0, A(1, 6))))) +A(0, A(0, A(0, A(0, A(0, A(1, 5)))))) +A(0, A(0, A(0, A(0, A(0, A(0, A(1, 4))))))) +A(0, A(0, A(0, A(0, A(0, A(0, A(0, A(1, 3)))))))) +A(0, A(0, A(0, A(0, A(0, A(0, A(0, A(0, A(1, 2))))))))) +A(0, A(0, A(0, A(0, A(0, A(0, A(0, A(0, A(0, A(1, 1))))))))) +A(0, A(0, A(0, A(0, A(0, A(0, A(0, A(0, A(0, 2)))))))) +A(0, A(0, A(0, A(0, A(0, A(0, A(0, A(0, 4))))))) +A(0, A(0, A(0, A(0, A(0, A(0, A(0, 8)))))) +A(0, A(0, A(0, A(0, A(0, A(0, 16))))) +A(0, A(0, A(0, A(0, A(0, 32)))) +A(0, A(0, A(0, A(0, 64))) +A(0, A(0, A(0, 128)) +A(0, A(0, 256) +A(0, 512) +1024 + +--------------------------------------------------------------------- + +A(2, 4) +A(1, A(2, 3)) +A(1, A(1, A(2, 2))) +A(1, A(1, A(1, A(2, 1)))) +A(1, A(1, A(1, 2))) +A(1, A(1, A(0, A(1, 1)))) +A(1, A(1, A(0, 2))) +A(1, A(1, 4)) +A(1, A(0, A(1, 3))) +A(1, A(0, A(0, A(1, 2)))) +A(1, A(0, A(0, A(0, A(1, 1))))) +A(1, A(0, A(0, A(0, 2))))) +A(1, A(0, A(0, 4)))) +A(1, A(0, 8))) +A(1, 16)) +A(0, A(1, 15))) +A(0, A(0, A(1, 14)))) +A(0, A(0, A(0, A(1, 13))))) +A(0, A(0, A(0, A(0, A(1, 12)))))) +A(0, A(0, A(0, A(0, A(0, A(1, 11))))))) +A(0, A(0, A(0, A(0, A(0, A(0, A(1, 10)))))))) +A(0, A(0, A(0, A(0, A(0, A(0, A(0, A(1, 9))))))))) +A(0, A(0, A(0, A(0, A(0, A(0, A(0, A(0, A(1, 8)))))))))) +A(0, A(0, A(0, A(0, A(0, A(0, A(0, A(0, A(0, A(1, 7))))))))))) +A(0, A(0, A(0, A(0, A(0, A(0, A(0, A(0, A(0, A(0, A(1, 6)))))))))))) +A(0, A(0, A(0, A(0, A(0, A(0, A(0, A(0, A(0, A(0, A(0, A(1, 5))))))))))))) +A(0, A(0, A(0, A(0, A(0, A(0, A(0, A(0, A(0, A(0, A(0, A(0, A(1, 4)))))))))))))) +A(0, A(0, A(0, A(0, A(0, A(0, A(0, A(0, A(0, A(0, A(0, A(0, A(0, A(1, 3))))))))))))))) +A(0, A(0, A(0, A(0, A(0, A(0, A(0, A(0, A(0, A(0, A(0, A(0, A(0, A(0, A(1, 2)))))))))))))))) +A(0, A(0, A(0, A(0, A(0, A(0, A(0, A(0, A(0, A(0, A(0, A(0, A(0, A(0, A(0, A(1, 1)))))))))))))))) +A(0, A(0, A(0, A(0, A(0, A(0, A(0, A(0, A(0, A(0, A(0, A(0, A(0, A(0, A(0, 2))))))))))))))) +A(0, A(0, A(0, A(0, A(0, A(0, A(0, A(0, A(0, A(0, A(0, A(0, A(0, A(0, 4)))))))))))))) +A(0, A(0, A(0, A(0, A(0, A(0, A(0, A(0, A(0, A(0, A(0, A(0, A(0, 8))))))))))))) +A(0, A(0, A(0, A(0, A(0, A(0, A(0, A(0, A(0, A(0, A(0, A(0, 16)))))))))))) +A(0, A(0, A(0, A(0, A(0, A(0, A(0, A(0, A(0, A(0, A(0, 32))))))))))) +A(0, A(0, A(0, A(0, A(0, A(0, A(0, A(0, A(0, A(0, 64)))))))))) +A(0, A(0, A(0, A(0, A(0, A(0, A(0, A(0, A(0, 128))))))))) +A(0, A(0, A(0, A(0, A(0, A(0, A(0, A(0, 256)))))))) +A(0, A(0, A(0, A(0, A(0, A(0, A(0, 512))))))) +A(0, A(0, A(0, A(0, A(0, A(0, 1024)))))) +A(0, A(0, A(0, A(0, A(0, 2048))))) +A(0, A(0, A(0, A(0, 4096)))) +A(0, A(0, A(0, 8192))) +A(0, A(0, A(0, 8192))) +A(0, A(0, 16384)) +A(0, 32768) +65536 + +--------------------------------------------------------------------- + +A(3, 3) +A(2, A(3, 2)) +A(2, A(2, A(3, 1))) +A(2, A(2, 2)) +A(2, A(1, A(2, 1))) +A(2, A(1, 2)) +A(2, A(0, A(1, 1))) +A(2, A(0, 2)) +A(2, 4) +From previous substitution then -> +65536 +*/ + +//Consider the following functions, where A is the function defined above: + +function f(n) { + return A(0,n); +} +function g(n) { + return A(1,n); +} +function h(n) { + return A(2,n); +} +function k(n) { + return 5 * n * n; +} + +//Give concise mathematical definitions for the functions computed by the functions f, g, and h for positive integer values of . For example, (k n) computes . + +//Solution + +/* +f(n) --> 2 * n +g(n) --> 2 ** n +h(n) --> 2 ** 2 ** 2 .. n times +k(n) --> 5 * (n ** 2) +*/ From 2822950b6f84d290c468e11a3011dbea98fe596c Mon Sep 17 00:00:00 2001 From: Ahmad El-Melegy Date: Thu, 29 Jan 2015 01:34:22 +0200 Subject: [PATCH 2/3] use A(1, 10) in 2nd problem, fix Indentation, add the term (Tetration) --- chapter-1/exercises/1.10.js | 44 ++++++++++++------------------------- 1 file changed, 14 insertions(+), 30 deletions(-) diff --git a/chapter-1/exercises/1.10.js b/chapter-1/exercises/1.10.js index d06c59d..b32ae7f 100644 --- a/chapter-1/exercises/1.10.js +++ b/chapter-1/exercises/1.10.js @@ -1,13 +1,14 @@ //Exercise 1.10. The following function computes a mathematical function called Ackermann’s function. function A(x,y) { - if (y === 0) - return 0; - else if (x === 0) - return 2 * y; - else if (y === 1) - return 2; - else return A(x - 1, A(x, y - 1)); + if (y === 0) + return 0; + else if (x === 0) + return 2 * y; + else if (y === 1) + return 2; + else + return A(x - 1, A(x, y - 1)); } /* What are the values of the following expressions? @@ -63,24 +64,7 @@ A(0, A(0, A(0, A(1, 13))))) A(0, A(0, A(0, A(0, A(1, 12)))))) A(0, A(0, A(0, A(0, A(0, A(1, 11))))))) A(0, A(0, A(0, A(0, A(0, A(0, A(1, 10)))))))) -A(0, A(0, A(0, A(0, A(0, A(0, A(0, A(1, 9))))))))) -A(0, A(0, A(0, A(0, A(0, A(0, A(0, A(0, A(1, 8)))))))))) -A(0, A(0, A(0, A(0, A(0, A(0, A(0, A(0, A(0, A(1, 7))))))))))) -A(0, A(0, A(0, A(0, A(0, A(0, A(0, A(0, A(0, A(0, A(1, 6)))))))))))) -A(0, A(0, A(0, A(0, A(0, A(0, A(0, A(0, A(0, A(0, A(0, A(1, 5))))))))))))) -A(0, A(0, A(0, A(0, A(0, A(0, A(0, A(0, A(0, A(0, A(0, A(0, A(1, 4)))))))))))))) -A(0, A(0, A(0, A(0, A(0, A(0, A(0, A(0, A(0, A(0, A(0, A(0, A(0, A(1, 3))))))))))))))) -A(0, A(0, A(0, A(0, A(0, A(0, A(0, A(0, A(0, A(0, A(0, A(0, A(0, A(0, A(1, 2)))))))))))))))) -A(0, A(0, A(0, A(0, A(0, A(0, A(0, A(0, A(0, A(0, A(0, A(0, A(0, A(0, A(0, A(1, 1)))))))))))))))) -A(0, A(0, A(0, A(0, A(0, A(0, A(0, A(0, A(0, A(0, A(0, A(0, A(0, A(0, A(0, 2))))))))))))))) -A(0, A(0, A(0, A(0, A(0, A(0, A(0, A(0, A(0, A(0, A(0, A(0, A(0, A(0, 4)))))))))))))) -A(0, A(0, A(0, A(0, A(0, A(0, A(0, A(0, A(0, A(0, A(0, A(0, A(0, 8))))))))))))) -A(0, A(0, A(0, A(0, A(0, A(0, A(0, A(0, A(0, A(0, A(0, A(0, 16)))))))))))) -A(0, A(0, A(0, A(0, A(0, A(0, A(0, A(0, A(0, A(0, A(0, 32))))))))))) -A(0, A(0, A(0, A(0, A(0, A(0, A(0, A(0, A(0, A(0, 64)))))))))) -A(0, A(0, A(0, A(0, A(0, A(0, A(0, A(0, A(0, 128))))))))) -A(0, A(0, A(0, A(0, A(0, A(0, A(0, A(0, 256)))))))) -A(0, A(0, A(0, A(0, A(0, A(0, A(0, 512))))))) +From previous substitution then -> A(1, 10) = 1024 A(0, A(0, A(0, A(0, A(0, A(0, 1024)))))) A(0, A(0, A(0, A(0, A(0, 2048))))) A(0, A(0, A(0, A(0, 4096)))) @@ -108,16 +92,16 @@ From previous substitution then -> //Consider the following functions, where A is the function defined above: function f(n) { - return A(0,n); + return A(0,n); } function g(n) { - return A(1,n); + return A(1,n); } function h(n) { - return A(2,n); + return A(2,n); } function k(n) { - return 5 * n * n; + return 5 * n * n; } //Give concise mathematical definitions for the functions computed by the functions f, g, and h for positive integer values of . For example, (k n) computes . @@ -127,6 +111,6 @@ function k(n) { /* f(n) --> 2 * n g(n) --> 2 ** n -h(n) --> 2 ** 2 ** 2 .. n times +h(n) --> 2 ** 2 ** 2 .. n times (Tetration) k(n) --> 5 * (n ** 2) */ From 6ca8fd6d47276867fa75df72b9edcdc6811f4164 Mon Sep 17 00:00:00 2001 From: Ahmad El-Melegy Date: Thu, 29 Jan 2015 01:40:57 +0200 Subject: [PATCH 3/3] use A(2, 2) in 3rd problem --- chapter-1/exercises/1.10.js | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/chapter-1/exercises/1.10.js b/chapter-1/exercises/1.10.js index b32ae7f..75666ce 100644 --- a/chapter-1/exercises/1.10.js +++ b/chapter-1/exercises/1.10.js @@ -64,7 +64,7 @@ A(0, A(0, A(0, A(1, 13))))) A(0, A(0, A(0, A(0, A(1, 12)))))) A(0, A(0, A(0, A(0, A(0, A(1, 11))))))) A(0, A(0, A(0, A(0, A(0, A(0, A(1, 10)))))))) -From previous substitution then -> A(1, 10) = 1024 +From previous substitution: A(1, 10) = 1024 A(0, A(0, A(0, A(0, A(0, A(0, 1024)))))) A(0, A(0, A(0, A(0, A(0, 2048))))) A(0, A(0, A(0, A(0, 4096)))) @@ -80,12 +80,9 @@ A(3, 3) A(2, A(3, 2)) A(2, A(2, A(3, 1))) A(2, A(2, 2)) -A(2, A(1, A(2, 1))) -A(2, A(1, 2)) -A(2, A(0, A(1, 1))) -A(2, A(0, 2)) +From previous substitution: A(2, 2) = 4 A(2, 4) -From previous substitution then -> +From previous substitution: A(2, 4) = 65536 65536 */