• Home
  • Textbooks
  • Computer science with Mathematica: theory and practice for science, mathematics, and engineering
  • Iteration and Recursion

Computer science with Mathematica: theory and practice for science, mathematics, and engineering

Roman Maeder

Chapter 3

Iteration and Recursion - all with Video Answers

Educators


Chapter Questions

11:58

Problem 1

With the following rules, we can find the gcd of two nonnegative integers.
$$
\begin{aligned}
& \operatorname{Gcd}\left[a_{-}, 0\right]:=a \\
& \operatorname{Gcd}\left[a_{-}, b_{-}\right]:=\operatorname{Gcd}[b, \operatorname{Mod}[a, b]]
\end{aligned}
$$
1. Find the evaluation steps for the example $\operatorname{Gcd}[8,13]$.
2. For which numbers $a$ and $b$, with $a \geq b>0$, are these rules applied exactly twice to find their gcd?
3. For which numbers less than 100 do we need the largest number of rule applications to find their gcd?

Raphael Tinoco
Raphael Tinoco
Numerade Educator

Problem 2

A third method for computing the ged (in addition to the two methods presented in Section 3.1) is obtained from the property $c|a \wedge c| b, a>b$ implies $c \mid a-b$.
1. What is the termination condition?
2. Find rules that implement this method, similar to the rules for the division method from Section 3.1.2.
3. Write a function with a While [] loop that uses this idea.
4. Find the loop invariant and prove correctness and termination using the methods outlined in Section 3.3.2.
Test your programs for a set of inputs that includes all special cases.

Check back soon!
04:03

Problem 3

In this exercise, we want to find numbers whose Collatz sequence is longer than the sequence of all other numbers in a certain range. See Section 3.2 for an explanation of Collatz sequences.
1. Write a function FindMax $\left[a_{-}, b_{-}\right]$that finds the maximal length of the Collatz sequence for all numbers between $a$ and $b$. The value of the function should be the number whose Collatz sequence is the longest.
2. Which number $n<1,000$ has the longest Collatz sequence?

Lucas Gagne
Lucas Gagne
Numerade Educator
03:12

Problem 4

Prove the correctness of the program $\mathrm{fibc}[n]$ from Section 7.2.2 (Listing 7.2-2) by following the methods given in Section 3.3.2.
$$
\begin{aligned}
& \\
& f i b c\left[n_{-}\right]:= \operatorname{Module}[\{f i=1, f i 1=0\} \\
&&\operatorname{Do}[\{f i, f i\}\}=\{f i+f i 1, f i\},\{n-1\}] ; \\
& f i
\end{aligned}
$$
Listing 7.2-2 A loop for the $n$th Fibonacci number.

Prathan Jarupoonphol
Prathan Jarupoonphol
Numerade Educator
11:07

Problem 5

The continued fraction expansion of an irrational number $r$ is the sequence $a_0, a_1, a_2, \ldots, a_i \in$ $\mathbf{N}$, such that (in the limit)
$$
r=a_0+\frac{1}{a_1+\frac{1}{a_2+\ldots}}
$$
The $a_i$ can be found as follows. Let $r_0=r$. The first term, $a_0$, is equal to the integer part of $r_0$ :
$$
a_0=\left\lfloor r_0\right\rfloor \text {. }
$$
The fractional part of $r_0$ is $r_0-a_0$. Its reciprocal, $r_1=1 /\left(r_0-a_0\right)$, is therefore
$$
r_1=a_1+\frac{1}{a_2+\frac{1}{a_3+\ldots}} .
$$
Thus, we get
$$
a_1=\left\lfloor r_1\right\rfloor
$$
and so on.
1. Write a function continuedFraction $\left[r_{-}, n_{-}\right.$Integer $]$that computes the first $n$ elements of the continued fraction expansion of $r$ as the list $\left\{a_0, a_1, \ldots, a_{n-1}\right\}$. You can use a loop or program recursively.
2. Write a function continuedValue[1_List] that returns the number belonging to the initial segment $l=\left\{a_0, a_1, \ldots, a_{n-1}\right\}$ of a continued fraction.
3. Write a simple definition for continuedError $[r, n]$, which finds the absolute error of the continued fraction approximation of length $n$. This error is the absolute value of the difference of $r$ and the approximation with $n$ terms.

Here are the first 10 elements of $\pi$ 's continued fraction expansion.
$$
\begin{aligned}
& \text { In }[1]:=\text { continuedFraction }[\mathrm{Pi}, 10] \\
& \text { Out }[1]=\{3,7,15,1,292,1,1,1,2,1\}
\end{aligned}
$$
The numerical value of the previous continued fraction is this rational number.
The numerical value of the previous continued fraction is this rational number.
$$
\begin{aligned}
& \text { In }[2]:=\text { continuedValue[\%] } \\
& \text { Out }[2]=\frac{1146408}{364913}
\end{aligned}
$$
Here is the error of the 10-term approxima-
$$
\begin{aligned}
& \operatorname{In}[3]:=N[\text { continuedBrror }[\mathrm{Pi}, 10]] \\
& \text { Out }[3]=1.6107110^{-12}
\end{aligned}
$$

Note that coth 1 has an interesting expansion.
$$
\begin{aligned}
& \text { In }[4]:=\text { continuedPraction }[\operatorname{Coth}[1], 12] \\
& \text { Out }[4]=\{1,3,5,7,9,11,13,15,17,19,21,23\}
\end{aligned}
$$

Chris Trentman
Chris Trentman
Numerade Educator