Assignment1 Solutions
Assignment1 Solutions
Assignment #1
Sample Solutions
Question 1:
1 if x ≥ 2 y + 3
P ( x, y ) =
0 otherwise
a) Write a program in the language L that computes P(x, y). You cannot use any macros
at all.
b) Write down the complete list of snapshots that occur during the computation of
P(5, 2). Use the format shown on page 29 in the textbook.
Answer a): The following program computes f(X1, X2), which is equivalent to P(x, y):
(1) Z1 ← Z1 + 1
(2) Z1 ← Z1 + 1
(3) Z1 ← Z1 + 1
(4) if X2≠0 GOTO A
(5) if Z1≠0 GOTO B
(6) [A] X2 ← X2 - 1
(7) Z1 ← Z1 + 1
(8) Z1 ← Z1 + 1
(9) if X2≠0 GOTO A
(10) [B] if X1≠0 GOTO C
(11) if Z1≠0 GOTO E
(12) [C] X1 ← X1 - 1
(13) Z1 ← Z1 - 1
(14) if X1≠0 GOTO C
(15) if Z1≠0 GOTO E
(16) Y←Y+1
Answer b):
Question 2:
Now you want to write a program in your favorite language L that computes the square
root function f(x) = √x. Remember that we only consider natural numbers; for example,
f(49) = 7, but f(17) = ↑.
a) Write down the program using macros. You are allowed to use the macros V ← V’, V
← m, GOTO L, and W ← f(V1, …, Vn). Use at least one macro of the form
W ← f(V1, …, Vn), and whenever you do so, provide a program that computes
f(V1, …, Vn).
b) In your program, expand all the macros of the form W ← f(V1, …, Vn) using the
convention given in Section 2.5 in the textbook. Write down the expanded version of
your program, which should now contain no macros except (possibly) those of the
forms V ← V’, V ← m, and GOTO L
Answer a):
IF X≠0 GOTO A
GOTO E
[A] Z1 ← X
Y←Y+1
Z2 ← SQ(Y) // SQ(Y) = Y⋅Y
[B] Z1 ← Z1 - 1
Z2 ← Z2 - 1
IF Z2≠0 GOTO C
IF Z1≠0 GOTO A
GOTO E
[C] IF Z1≠0 GOTO B
GOTO C
IF X≠0 GOTO A
GOTO E
[A] Z1 ← X
[B] Z2 ← X
[C] Z2 ← Z2 - 1
Y ← Y+ 1
IF Z2≠0 GOTO C
Z1 ← Z1 - 1
IF Z1≠0 GOTO B
Answer b):
IF X≠0 GOTO A
GOTO E
[A] Z1 ← X
Y←Y+1
Z3 ← 0
Z4 ← Y
Z5 ← 0
Z6 ← 0
IF Z4≠0 GOTO A4
GOTO E3
[A4] Z5 ← Z4
[A5] Z6 ← Z4
[A6] Z6 ← Z6 - 1
Z3 ← Z3 + 1
IF Z6≠0 GOTO A6
Z5 ← Z5 - 1
IF Z5≠0 GOTO A5
[E3] Z2 ← Z3
[B] Z1 ← Z1 - 1
Z2 ← Z2 - 1
IF Z2≠0 GOTO C
IF Z1≠0 GOTO A
GOTO E
[C] IF Z1≠0 GOTO B
GOTO C
Question 3:
Give a detailed argument that the following functions are primitive recursive:
a) xy
b) the predecessor function p(x)
These arguments should be of the same form as those provided in Examples 1, 2, and 3 of
Section 3.4 in the textbook.
a) h(x, y) = xy
h(x, 0) = 1
h(x, y+1) = h(x, y)⋅x
Step 2: Write recursive equations in terms of initial functions
h(x, 0) = s(n(x))
h(x, y+1) = g(y, h(x,y), x)
where
g(x1, x2, x3) = u23(x1, x2, x3) ⋅u33(x1, x2, x3)
_
h(0) = 0
h(t+1) = t
h(0) = n(t)
h(t+1) = g(t, h(t))
where
g(x1, x2) = u12(x1, x2)
Question 4:
Requirement 1 is fulfilled, because all three initial functions are total functions, so they
belong to T .
Requirement 2 is also fulfilled, and we will show this separately for composition and
recursion:
Composition: Let h(x1,..., xn) = f(g1(x1,..., xn),..., gk(x1,..., xn)), where f and the functions
gi are members of T . The domain of h is the domain of all gi. Because all functions gi are
total, they will yield k input values to f for any given x1,..., xn. Since f is total, it is defined
for any g1,..., gk. Therefore, h is defined for all x1,..., xn, which means that h is total.
Recursion: Let
h(x1,..., xn, 0) = f(x1,..., xn)
h(x1,..., xn, t + 1) = g(t, h(x1,..., xn, t), x1,..., xn),
where f and g are in T . Then we can show by induction that h is total:
Basis step: For t = 0, h(x1,..., xn, 0) is defined.
Induction step: Suppose that h(x1,..., xn, t) is defined. Then, because g is total,
g(t, h(x1,..., xn, t), x1,..., xn) is defined, and therefore, h(x1,..., xn, t + 1) is also defined.
Conclusion: h is total.
Question 5:
Let f(x) be the sum of the divisors of x if x ≠ 0; f(0) = 0. For example, f(6) = 1 + 2 + 3 + 6
= 12. Show that f(x) is primitive recursive.
We know from the theorems in the textbook that the operations used in this equation are
primitive recursive. Since we use composition of these functions to obtain f, f must also
be primitive recursive.
Question 6:
Let u(n) be the n-th number in order of size which is the sum of two squares. Show that
u(n) is primitive recursive.
u(0) = 0
u(n+1) = mint≤n2 [(∃x) ≤t(∃y) ≤t(x2 + y2 = t) & (t > u(n) ∨ n = 0)]
We know from the theorems in the textbook that the operations used in this equation are
primitive recursive. Since we apply composition and recursion to these functions to
obtain u, u must also be primitive recursive.
However, we still have to show that u(n+1) ≤ n2 for all n.
s(0) = 0
s(1) = 02 = 0
s(2) = 12 = 1
s(3) = 22 = 4
s(4) = 32 = 9
.
.
.
Obviously, we have s(n+1) ≤ n2 for all n. Now if we consider u(n), it contains all the
numbers in s(n), but also additional ones that need to be listed (such as 12 + 12 = 2), so
with larger n the values in the sequence u(n) increase more slowly than those in s(n).
Therefore, we have u(n) ≤ s(n) for all n, and since s(n+1) ≤ n2, we finally get
u(n+1) ≤ n2.