# more examples of primitive recursive functions

With bounded minimization, bounded maximization, and properties of primitive recursive predicates, many more examples of primitive recursive functions can now be exhibited. We list some of the most common ones:

1. 1.

$\operatorname{NxtPrm}(n)$ is the smallest prime number greater than or equal to $n$.

Let $\Phi_{1}(y)$ be the predicate$y$ is prime” and $\Phi_{2}(n,y)$ the predicate “$n\leq y$”. Then

 $\operatorname{NxtPrm}(n)=\mu y(\Phi_{1}(y)\wedge\Phi_{2}(n,y)),$

where $\mu$ is the minimization operator. Since both $\Phi_{1}$ and $\Phi_{2}$ are primitive recursive, $\operatorname{NxtPrm}$ is recursive. From Euclid’s proof of the infinitude of primes (http://planetmath.org/ProofThatThereAreInfinitelyManyPrimes), two conclusions can be made:

• $\operatorname{NxtPrm}$ is a total function, and

• the search for $y$ need not be unbounded: let $p_{1},\ldots,p_{k}$ be all the prime numbers less than $n$ (suppose $n>2$), then $p=p_{1}\cdots p_{k}+1$ does not divide any of the $p_{i}$, so must be at least $n$. Let $q$ be some prime such that $q|p$. Then $q\neq p_{i}$ for any $i=1,\ldots,k$, which means that $n\leq q$. So what we have shown is that $q$ is the most we need to search to find the next prime after $n$. Since $q\leq p=p_{1}\cdots p_{k}+1\leq n!+1$, we may reformulate the above expression:

 $\operatorname{NxtPrm}(n)=\mu y\leq(n!+1)(\Phi_{1}(y)\wedge\Phi_{2}(n,y)).$

Since $n!+1$ is primitive recursive, we conclude that $\operatorname{NxtPrm}$ is primitive recursive by one application of bounded minimization and functional composition.

2. 2.

$\operatorname{Pr}(n)$ is the $n$-th prime number, and $\operatorname{Pr}(0):=1$. Again, this is a total function. To see that $\operatorname{Pr}$ is primitive recursive, we simply note that $\operatorname{Pr}(n+1)=\operatorname{NxtPrm}(\operatorname{Pr}(n))$.

3. 3.

$\operatorname{div}(x,y)$, which is $1$ if $x|y$, and $0$ otherwise, is primitive recursive. The predicate $\Phi(x,y,z)$$x=yz$” is primitive recursive, so that $f(x,y):=\mu z\leq x\Phi(x,y,z)$ is primitive recursive. $f(x,y)$ returns $z\leq x$ if $yz=x$, and $x+1$ otherwise. Thus “$f(x,y)\leq x$” is a primitive recursive predicate, and its characteristic function is easily seen to be $\operatorname{div}(x,y)$, hence primitive recursive.

Note that the least $z$ such that $x=yz$ is also the only $z$ satisfying the equation. An alternative, more direct (without bounded minimization) way to prove that $\operatorname{div}$ is primitive recursive is by noticing that $\operatorname{div}(x,y)=1\dot{-}\operatorname{sgn}(\operatorname{rem}(x,y))$, where $\operatorname{sgn}$ is the sign function, and $\operatorname{rem}$ is the remainder function, both of which are primitive recursive. Since $\dot{-}$ is primitive recursive, so is $\operatorname{div}$.

Remark. $\operatorname{div}(x,y)$ is also the characteristic function of the predicate “$x|y$”, which shows that “$x$ divides $y$” is a primitive recursive predicate.

4. 4.

$[x^{1/y}]$, which returns the largest $z$ (bounded by $x$) such that $z^{y}\leq x$, is primitive recursive, since it can be obtained by an application of bounded maximization to the predicate “$z^{y}\leq x$”.

5. 5.

$\operatorname{lo}(x,y)$, which returns the largest $z$ (bounded by $y$) such that $x^{z}\mid y$, the integer version of the logarithm function, is primitive recursive, for $\operatorname{lo}(x,y)$ is the largest $z$ such that $\operatorname{div}(x^{z},y)=1$, and $\operatorname{div}$ has been shown to be primitive recursive in the previous example. To make $\operatorname{lo}$ a total function, we also set $\operatorname{lo}(x,y):=0$ if $x\in\{0,1\}$.

6. 6.

$F(n)$ is the $n$-th Fibonacci number. The Fibonacci numbers are defined using a slight variation of primitive recursion: $F(0)=F(1)=1$, and $F(n+2)=F(n+1)+F(n)$. To show that $F$ is primitive recursive, first define a function $g$ as follows:

 $g(n)=\operatorname{exp}(2,F(n))\operatorname{exp}(3,F(n+1)).$

Then $F(n)=\operatorname{lo}(2,g(n))$, and $F(n+1)=\operatorname{lo}(3,g(n))$. Moreover, $g(0)=6$, and

 $\displaystyle g(n+1)$ $\displaystyle=$ $\displaystyle\operatorname{exp}(2,F(n+1))\operatorname{exp}(3,F(n+2))$ $\displaystyle=$ $\displaystyle\operatorname{exp}(2,F(n+1))\operatorname{exp}(3,F(n+1)+F(n))$ $\displaystyle=$ $\displaystyle\operatorname{exp}(2,\operatorname{lo}(3,g(n)))\operatorname{exp}% (3,\operatorname{lo}(3,g(n))+\operatorname{lo}(2,g(n))).$

Thus $g$ is defined via primitive recursion from multiplication, exponentiation, and $\operatorname{lo}$, all of which primitive recursive. Therefore, $g(n)$, and consequently, $F(n)=\operatorname{lo}(2,g(n))$, is primitive recursive. The type of recursion used in defining the Fibonacci numbers is known as course-of-values recursion.

7. 7.

$\operatorname{gcd}(x,y)$ is the greatest common divisor of $x$ and $y$ (for convenience, we set $\operatorname{gcd}(x,0)=\operatorname{gcd}(0,y):=0$). In other words, the $\operatorname{gcd}$ function is defined by two cases:

 $\operatorname{gcd}(x,y):=\left\{\begin{array}[]{ll}0&\textrm{if }xy=0,\\ z&\textrm{if }xy\neq 0\mbox{ and }z\mbox{ is the largest number }\!\!\leq x% \mbox{ with }z|x\mbox{ and }z|y.\end{array}\right.$

Since both $z|x$ and $z|y$ are primitive recursive (from previous example), as well as the predicate “$xy\neq 0$”, so is their conjunction. Let $c(x,y,z)$ be the corresponding characteristic function. By taking the bounded maximum, the second case of $\operatorname{gcd}$ is primitive recursive. Since the first case of $\operatorname{gcd}$ is clearly primitive recursive, hence $\operatorname{gcd}$ itself is primitive recursive.

8. 8.

As a result, the predicate “$x$ and $y$ are coprime” is primitive recursive, as it has the same characteristic function as the predicate “$\operatorname{gcd}(x,y)=1$”, and $\operatorname{gcd}(x,y)$ has just been shown to be primitive recursive.

9. 9.

Euler $\phi$-function (http://planetmath.org/EulerPhifunction) is primitive recursive. Let $c(x,y)$ be the characteristic function of the primitive recursive predicate “$x$ and $y$ are coprime”. Consider the bounded sum

 $f(x,y)=\sum_{i=0}^{y}c(x,i),$

which is primitive recursive. Hence $\phi(x)=f(x,x)$ is too. Note that $\phi(0)=0$.

Remark. It is not hard to show that, all of the functions above are in fact elementary recursive.

Title more examples of primitive recursive functions MoreExamplesOfPrimitiveRecursiveFunctions 2013-03-22 19:06:02 2013-03-22 19:06:02 CWoo (3771) CWoo (3771) 13 CWoo (3771) Example msc 03D20