Chapter 18

Algorithms and Programs

[9/100%]
Show
LaTeX
Problem 280

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

?
(a)

dec⁡(x)=⌊x−1⌋\operatorname {dec}(x)=\lfloor x-1\rfloor, where ⌊0−1⌋=0\lfloor 0-1\rfloor =0 and ⌊(x+1)−1⌋=x\lfloor (x+1)-1\rfloor =x;

(b)

sub⁡(x,y)=⌊x−y⌋\operatorname {sub}(x, y)=\lfloor x-y\rfloor, where ⌊x−y⌋=x−y\lfloor x-y\rfloor =x-y if x⩾yx \geqslant y, and ⌊x−y⌋=0\lfloor x-y\rfloor =0 otherwise;

(c)

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

(d)

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

(e)

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

(f)

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

(g)

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

(h)

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

(i)

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

Problem 281

Construct a program with labels that, given a natural number ii, computes the Fibonacci number FiF_{i}.

?
Problem 282

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

?
(a)

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

(b)

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

(c)

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

(d)

f4(x,y)={⌊log⁡3(x+y+1)⌋ if 2x⩽y3+y,⌊x+y⌋ otherwise; f_{4}(x, y)= \begin{cases} \left\lfloor \log_{3}(x+y+1)\right\rfloor & \text{ if } 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}

Problem 283

Let a program П with a single 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 VΠ={x,y,z1,…,zm}\boldsymbol {V}_{\Pi }=\left\{ x, y, z_{1}, \ldots , z_{m}\right\}. Construct a program that computes the function inverse to ff: f−1(y)=xf^{-1}(y)=x if f(x)=yf(x)=y.

?
Problem 284

Let П be a program with ∣VΠ∣=m\left|\boldsymbol {V}_{\Pi }\right|=m. It follows from the definitions that, for different choices of input 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 having a different number of arguments are considered distinct a priori.

(b)

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

(c)

Construct a program Πm,∣VΠm∣=m\Pi_{m},\left|\boldsymbol {V}_{\Pi_{m}}\right|=m, that computes, for each n⩽mn \leqslant m, the maximum number of different functions of nn variables.

Problem 285

Solve the previous problem in the case where the possible input variables are ordered in advance.

?
Problem 286

Determine how many pairwise non-equivalent programs with labels there are, having at most nn statements and only one variable, which is simultaneously the input and output variable.

?
Problem 287

Determine which everywhere-defined functions can be computed by bounded programs with labels that have only one variable, which is simultaneously the input and output variable.

?
Problem 288

Let a program with labels contain only propositional variables x1,…,xnx_{1}, \ldots , x_{n} and assignments with Boolean connectives (similarly to the linear programs of Section 13.2). Propose a method that allows one to determine which function such a program computes.

?