Chapter 22

Program Computability

[20/100%]
Show
LaTeX
Problem 538

Determine which of the following strings are syntactically correct structured programs:

?
(a)

x←s(x);z←y;u←(s(x)<z)x \leftarrow s(x) ; z \leftarrow y ; \quad u \leftarrow (s(x)<z); if uu then y←zy \leftarrow z; else y←xy \leftarrow x; end;

(b)

x←s(x);z←y;z←s(z)x \leftarrow s(x) ; z \leftarrow y ; z \leftarrow s(z); if x←zx \leftarrow z then y←zy \leftarrow z; else y←xy \leftarrow x; end;

(c)

x←y;u←z;u←s(u);x←(u>z)x \leftarrow y ; \quad u \leftarrow z ; \quad u \leftarrow s(u) ; \quad x \leftarrow (u>z); while xx do y←zy \leftarrow z; u←s(u)u \leftarrow s(u); end;

(d)

x←s(z);z←y;x←(x<x)x \leftarrow s(z) ; z \leftarrow y ; x \leftarrow (x<x); if xx then y←zy \leftarrow z; else x←yx \leftarrow y; end;

(e)

x←x;x←x;y←y;y←yx \leftarrow x ; x \leftarrow x ; y \leftarrow y ; y \leftarrow y;

(f)

x←y;u←z;u←s(u);x←(z<u)x \leftarrow y ; \quad u \leftarrow z ; \quad u \leftarrow s(u) ; \quad x \leftarrow (z<u); while xx do y←zy \leftarrow z; u←s(u);u \leftarrow s(u) ; end ;x←y;; x \leftarrow y ;

(g)

x←s(x);z←y;z←s(z);x←(x←z)x \leftarrow s(x) ; \quad z \leftarrow y ; \quad z \leftarrow s(z) ; \quad x \leftarrow (x \leftarrow z); if xx then y←zy \leftarrow z; z←s(z)z \leftarrow s(z); else y←xy \leftarrow x; end; ⊛\circledast

Problem 539

For the following structured program Π\Pi, find Π(σ)\Pi (\sigma ), where σ={(x,3);(y,4);(z,2)}\sigma =\left\{ (x, 3) ;(y, 4) ;(z, 2)\right\}: ⊛\circledast

x←y;x←s(x);y←z;y←s(y);z←s(z);y←s(x);z←y;z←s(z);x←s(x); \begin{aligned} x \leftarrow y ; x \leftarrow s(x) ; y \leftarrow z ; y & \leftarrow s(y) ; z \leftarrow s(z) ; \\ y & \leftarrow s(x) ; z \leftarrow y ; z \leftarrow s(z) ; x \leftarrow s(x) ; \end{aligned}
?
Problem 540

For the structured program П in Fig. 34 (left) find П(σ)П(\sigma ), where σ={(x,0);(y,3);(z,5);(u,4);(v,2)}\sigma =\left\{ (x, 0) ;(y, 3) ;(z, 5) ;(u, 4); (v, 2)\right\}. ⊛\circledast

x←y;x←s(x);y←u;x \leftarrow y ; x \leftarrow s(x) ; y \leftarrow u ;
y←s(y);v←z;v←s(v);y \leftarrow s(y) ; v \leftarrow z ; v \leftarrow s(v) ;
u←(x<v);v←(x<y);u \leftarrow (x<v) ; v \leftarrow (x<y) ;
ifuu then
\quad ifvv then
z←y;z←s(z);\quad \quad z \leftarrow y ; z \leftarrow s(z) ;
\quad else
z←x;\quad \quad z \leftarrow x ;
\quad end;
else
z←s(x);\quad z \leftarrow s(x) ;
end;
: Fig. 34 (left): Program from Problems 540 and 541.
?
Problem 541

For the structured program П in Fig. 34 (right) find Π(σ)\Pi (\sigma ), where σ={(x,2);(y,3);(z,7);(u,5);(v,0)}\sigma =\left\{ (x, 2) ;(y, 3) ;(z, 7) ;(u, 5); (v, 0)\right\}. ⊛\circledast

x←y;x←s(x);v←u;x \leftarrow y ; x \leftarrow s(x) ; v \leftarrow u ;
v←s(v);v \leftarrow s(v) ;
z←(x<v);z \leftarrow (x<v) ;
whilezz do
z←(y<x);\quad z \leftarrow (y<x) ;
\quad ifzz then
y←s(y);\quad \quad y \leftarrow s(y) ;
\quad else
x←s(x);u←s(u);\quad \quad x \leftarrow s(x) ; u \leftarrow s(u) ;
\quad end;
z←(x<v);\quad z \leftarrow (x<v) ;
end;
: Fig. 34 (right): Program from Problems 540 and 541.
?
Problem 542

Write a structured program Π+\Pi_{+} that has input variables xx and yy and computes the function x+yx+y in the variable xx, without changing yy. ⊛\circledast

?
Problem 543

Let Π+\Pi_{+} be a program that computes the function x+yx+y in the variable xx, without changing yy and using additional variables z0z_{0} and z1z_{1}. Which of the structured programs in Fig. 35 compute the product xyx y in the variable xx? ⊛\circledast

z3←xz_{3} \leftarrow x;z3←y;y←x;z_{3} \leftarrow y ; y \leftarrow x ;
z2←0;x←0;z_{2} \leftarrow 0 ; x \leftarrow 0 ;x←0;x \leftarrow 0 ;
z1←(z2<z3);z_{1} \leftarrow \left(z_{2}<z_{3}\right) ;z0←(z2<z3);z_{0} \leftarrow \left(z_{2}<z_{3}\right) ;
whilez1z_{1} dowhilez0z_{0} do
z1←0;Π+\quad z_{1} \leftarrow 0 ; \Pi _{+}z0←0;z1←0;\quad z_{0} \leftarrow 0 ; z_{1} \leftarrow 0 ;
z0←0\quad z_{0} \leftarrow 0;Π+z2←s(z2);\quad \Pi _{+} z_{2} \leftarrow s\left(z_{2}\right) ;
z2←s(z2);\quad z_{2} \leftarrow s\left(z_{2}\right) ;z0←(z2<z3);\quad z_{0} \leftarrow \left(z_{2}<z_{3}\right) ;
z1←(z2<z3);\quad z_{1} \leftarrow \left(z_{2}<z_{3}\right) ;end;
end;
: Fig. 35: Programs from Problem 543.
?
Problem 544

Let Π×\Pi_{\times } be a program that computes the function xyx y in the variable xx, using auxiliary variables zi,i=0,1,…,mz_{i}, i=0,1, \ldots , m, as constructed in Problem 543. Πz\Pi_{z} is the program that zeros out the variables zi:z0←0;…zm←0z_{i}: z_{0} \leftarrow 0 ; \ldots z_{m} \leftarrow 0;. Which of the structured programs in Fig. 36 compute in the variable xx the square root of xx, that is, the function sqrt⁡(x)=⌊x⌋\operatorname {sqrt}(x)=\lfloor \sqrt{x}\rfloor? ⊛\circledast

u←x;x←0;u \leftarrow x ; x \leftarrow 0 ;u←x;u \leftarrow x ;u←x;u←s(u);u \leftarrow x ; u \leftarrow s(u) ;
whilexx dowhilexx dowhilexx do
ΠzΠ×\quad \Pi _{z} \Pi _{\times }y1←y;\quad y_{1} \leftarrow y ;y1←y;\quad y_{1} \leftarrow y ;
y1←y;\quad y_{1} \leftarrow y ;y←s(y);\quad y \leftarrow s(y) ;y←s(y);\quad y \leftarrow s(y) ;
y←s(y);\quad y \leftarrow s(y) ;x←y;\quad x \leftarrow y ;x←y;\quad x \leftarrow y ;
x←(x<u);\quad x \leftarrow (x<u) ;ΠzΠ×\quad \Pi _{z} \Pi _{\times }ΠzΠ×\quad \Pi _{z} \Pi _{\times }
end;x←(x<u);\quad x \leftarrow (x<u) ;x←(x<u);\quad x \leftarrow (x<u) ;
u←(u<x);u \leftarrow (u<x) ;end;end;
ifuu thenu←(u<x);u \leftarrow (u<x) ;x←y1;x \leftarrow y_{1} ;
x←y1;\quad x \leftarrow y_{1} ;x←y;x \leftarrow y ;
elseifuu then
x←y;\quad x \leftarrow y ;x←y1;\quad x \leftarrow y_{1} ;
end;end;
: Fig. 36: Programs from Problem 544.
?
Problem 545

Let Π×\Pi_{\times } and Πz\Pi_{z} be the programs from Problem 544. Which of the structured programs in Fig. 37 compute in the variable xx the integer part of the quotient: ⌊x/y⌋\lfloor x / y\rfloor? For y=0y=0 the result should also equal zero. ⊛\circledast

u←s(x);x←0;u \leftarrow s(x) ; x \leftarrow 0 ;u←s(x);x←0;i←yu \leftarrow s(x) ; x \leftarrow 0 ; i \leftarrow y;u←x;i←y;u \leftarrow x ; i \leftarrow y ;
i←(x<u);i \leftarrow (x<u) ;whileii dowhileii do
whileii dox←j;\quad x \leftarrow j ;j1←j;\quad j_{1} \leftarrow j ;
j1←j;\quad j_{1} \leftarrow j ;ΠzΠ×\quad \Pi _{z} \Pi _{\times }j←s(j);\quad j \leftarrow s(j) ;
j←s(j);\quad j \leftarrow s(j) ;i←(x<u);\quad i \leftarrow (x<u) ;x←j;\quad x \leftarrow j ;
ΠzΠ×\quad \Pi _{z} \Pi _{\times }ifii thenΠzΠ×\quad \Pi _{z} \Pi _{\times }
i←(x<u);\quad i \leftarrow (x<u) ;j1←j;\quad j_{1} \leftarrow j ;i←(x<u);\quad i \leftarrow (x<u) ;
end;end;end;
x←j1;x \leftarrow j_{1} ;j←s(j);j \leftarrow s(j) ;i←(u<x);i \leftarrow (u<x) ;
i←(j<u);i \leftarrow (j<u) ;x←j;x \leftarrow j ;
end;ifii then
x←j1;\quad x \leftarrow j_{1} ;
end;
: Fig. 37: Programs from Problem 545.
?
Problem 546

Construct programs that compute the following functions in the variable xx, and prove their correctness. For two-place functions, the value of the variable yy must remain unchanged.

?
(a)

mult⁡(x,y)=xy\operatorname {mult}(x, y)=x y;

(b)

dec⁡(x)=x÷1\operatorname {dec}(x)=x \div 1, where 0÷1=00 \div 1=0 and (x+1)÷1=x(x+1) \div 1=x;

(c)

sub⁡(x,y)=x÷y\operatorname {sub}(x, y)=x \div y, where x÷y=x−yx \div y=x-y if x⩾yx \geqslant y, and x÷y=0x \div y=0 otherwise;

(d)

fact⁡(x)=x!\operatorname {fact}(x)=x! — factorial;

(e)

div⁡(x,y)=⌊x/y⌋\operatorname {div}(x, y)=\lfloor x / y\rfloor — integer quotient;

(f)

sqrt⁡(x)=⌊x⌋\operatorname {sqrt}(x)=\lfloor \sqrt{x}\rfloor;

(g)

pow⁡(x,y)=xy\operatorname {pow}(x, y)=x^{y} — exponentiation;

(h)

log⁡(y,x)=⌊log⁡yx⌋\log (y, x)=\left\lfloor \log_{y} x\right\rfloor;

(i)

 mod (x,y)\bmod (x, y) — the remainder of dividing xx by yy;

(j)

τ(x)\tau (x) — the number of distinct divisors of xx, τ(0)=0\tau (0)=0. ⊛\circledast

Problem 547

Construct a program with labels that computes the Fibonacci number FiF_{i} from a natural number ii. ⊛\circledast

?
Problem 548

Construct programs that compute the following functions in the variable zz:

?
(a)

f1(x,y)={⌊(x+y)/2⌋ when x<y,x2y3 otherwise; f_{1}(x, y)= \begin{cases} \lfloor (x+y) / 2\rfloor & \text{ when } x<y, \\ x^{2} y^{3} & \text{ otherwise; }\end{cases}

(b)

f2(x,y)={3y when log⁡2(x+1)⩾y,∣x−y∣ otherwise; f_{2}(x, y)= \begin{cases} 3^{y} & \text{ when } \log_{2}(x+1) \geqslant y, \\ \left|x-y\right| & \text{ otherwise; }\end{cases}

(c)

f3(x,y)={⌊xy/2⌋ when log⁡2x⩽y+2,⌊x⌊y/2⌋⌋ otherwise; f_{3}(x, y)= \begin{cases} \lfloor x y / 2\rfloor & \text{ when } \log_{2} x \leqslant y+2, \\ \left\lfloor x^{\lfloor y / 2\rfloor }\right\rfloor & \text{ otherwise; }\end{cases}

(d)

f4(x,y)={⌊log⁡3(x+y+1)⌋ when 2x⩽y3+y,⌊x+y⌋ otherwise; f_{4}(x, y)= \begin{cases} \left\lfloor \log_{3}(x+y+1)\right\rfloor & \text{ when } 2 x \leqslant y^{3}+y, \\ \lfloor \sqrt{x+y}\rfloor & \text{ otherwise; }\end{cases}

(e)

f5(x,y)={1, if x is a prime number, 0 otherwise. f_{5}(x, y)= \begin{cases} 1, & \text{ if } x \text{ is a prime number, } \\ 0 & \text{ otherwise. }\end{cases} ⊛\circledast

Problem 549

For each of the functions A(x,y),B(x,y),C(x,y),D(x,y),E(x,y),G(x,y)A(x, y), B(x, y), C(x, y), D(x, y), E(x, y), G(x, y), and H(x,y)H(x, y) given below by recursive relations, construct a structured program computing it:

?
(a)

A(0,y)=y+1A(0, y)=y+1,

A(x+1,0)=2A(x,1)A(x+1,y+1)=A(x,y)+xy \begin{aligned} & A(x+1,0)=2 A(x, 1) \\ & A(x+1, y+1)=A(x, y)+x y \end{aligned}
(b)

B(0,y)=2yB(0, y)=2 y,

B(x+1,0)=B(x,1)+1B(x+1,y+1)=x+B(x+1,y) \begin{aligned} & B(x+1,0)=B(x, 1)+1 \\ & B(x+1, y+1)=x+B(x+1, y) \end{aligned}
(c)

C(0,y)=y+2C(0, y)=y+2,

C(x+1,0)=C(x,1)+1C(x+1,y+1)=C(x+1,y)−x \begin{aligned} & C(x+1,0)=C(x, 1)+1 \\ & C(x+1, y+1)=C(x+1, y)-x \end{aligned}
(d)

D(x,0)=x,D(0,y)=yD(x, 0)=x, D(0, y)=y,

D(x+1,y+1)=D(x,y+1)+xy; D(x+1, y+1)=D(x, y+1)+x^{y} ;
(e)

E(0,y)=y+1E(0, y)=y+1, E(x+1,0)=E(x,1)E(x+1,0)=E(x, 1), E(x+1,y+1)=xE(x+1,y);E(x+1, y+1)=x E(x+1, y) ;

(f)

G(0,y)=3G(0, y)=3, G(x+1,0)=G(x,0)+⌊log⁡2(x+1)⌋G(x+1,0)=G(x, 0)+\left\lfloor \log_{2}(x+1)\right\rfloor, G(x+1,y+1)=G(x+1,y)+x2y;G(x+1, y+1)=G(x+1, y)+x^{2} y ;

(g)

H(0,y)=yH(0, y)=y, H(x+1,0)=H(x,1)+2H(x+1,0)=H(x, 1)+2, H(x+1,1)=H(x,0)+1H(x+1,1)=H(x, 0)+1, H(x+1,y+2)=y+H(x+1,y)H(x+1, y+2)=y+H(x+1, y). ⊛\circledast

Problem 550

Write a program that implements the assignment a←(b<c)a \leftarrow (b<c);, using assignments of the forms x←0;x←yx \leftarrow 0 ; x \leftarrow y; x←s(y);x \leftarrow s(y) ; and x←eq⁡(y,z);x \leftarrow \operatorname {eq}(y, z) ;. The latter changes the value of xx to 1 if the values of yy and zz are equal, or to 0 otherwise. ⊛\circledast

?
Problem 551

Prove that for computing any computable function one can write a structured program without branching. Hint. First show that one can write a program without complete branches. ⊛\circledast

?
Problem 552

Let a program П with one input variable xx compute in the variable yy some everywhere-defined one-to-one function ff whose range coincides with the set of all natural numbers ω\omega. Let Var⁡[Π]={x,y,z1,…,zm}\operatorname {Var}\left[\Pi \right]=\left\{ x, y, z_{1}, \ldots , z_{m}\right\}. Construct a program that computes the inverse function to ff: f−1(y)=xf^{-1}(y)=x, if f(x)=yf(x)=y. ⊛\circledast

?
Problem 553

Let П be a program with ∣Var⁡[Π]∣=m\left|\operatorname {Var}\left[\Pi \right]\right|=m. From the definitions it follows that, for different choices of input variables and output variables, the program can compute different functions.

?
(a)

What is the maximum number of functions of n⩽mn \leqslant m variables that П can compute? How many different functions in total can П compute? Functions with different numbers of arguments are considered different a priori.

(b)

Construct a program Πm,n\Pi_{m, n} that computes the maximum number of distinct functions of n⩽mn \leqslant m variables.

(c)

Construct a program Πm,∣Var⁡[Πm]∣=m\Pi_{m},\left|\operatorname {Var}\left[\Pi_{m}\right]\right|=m, that for every n⩽mn \leqslant m computes the maximum number of distinct functions of nn variables. ⊛\circledast

Problem 554

Determine how many pairwise nonequivalent programs with labels there are in total, having at most nn statements and only one variable that is simultaneously an input and an output variable. ⊛\circledast

?
Problem 555

Determine which everywhere-defined functions can be computed by programs with labels that have only one variable, which is simultaneously an input and an output variable, and use only branches and assignments of the forms x←s(x)x \leftarrow s(x); and x←dec⁡(x)x \leftarrow \operatorname {dec}(x);. ⊛\circledast

?
Problem 556

Let a program with labels contain only propositional variables x1,…,xnx_{1}, \ldots , x_{n}, branches, and assignments with Boolean connectives: xi←xj∧xk;xi←xj∨xk;xi←xj→xk;xi←¬xjx_{i} \leftarrow x_{j} \wedge x_{k} ; x_{i} \leftarrow x_{j} \vee x_{k} ; x_{i} \leftarrow x_{j} \rightarrow x_{k} ; x_{i} \leftarrow \neg x_{j};. Propose a way to determine which function such a program computes. ⊛\circledast

?
Problem 557

Prove that none of the functions x+y,x÷y,xyx+y, x \div y, x y, and ⌊x/y⌋\lfloor x / y\rfloor can be computed by any structured program without loops. ⊛\circledast

?