• Keine Ergebnisse gefunden

Introduction to Mathematical Software 4

N/A
N/A
Protected

Academic year: 2022

Aktie "Introduction to Mathematical Software 4"

Copied!
2
0
0

Wird geladen.... (Jetzt Volltext ansehen)

Volltext

(1)

Introduction

to Mathematical Software 4 th Exercise Sheet

Department of Mathematics winter term 2009/2010

PD Dr. Ulf Lorenz 23/11/2009

Christian Brandenburg

Important Notice

Before continuing with this exercise sheet, you should have finishedExercises 2.1 and 2.2, as well asExercise 3.1. If you had difficulties with the recursion inExercise 3.2, have a look atExercise 4.1and go back toExercise 3.2afterwards.

The exercises marked with**are advanced exercises for students who already have programming experience but, of course, everybody can give them a try.

If you still have difficulties with or questions about the material covered in these exercises, don’t hesitate to ask questions or join the office hours!

Exercise 4.1Recursion – Fibonacci Numbers

In this exercise we consider the recursive definition of the Fibonacci numbers, which is given by Fib(n+2) =Fib(n+1) +Fib(n)

Fib(0) =1 Fib(1) =1 forn∈N.

From this definition, we can easily derive a recursive algorithm (given in pseudocode):

Function 1Fib(n) ifn < 2then

return 1 else

return Fib(n-1) + Fib(n-2) end if

As you can see, forn≥2, the functionFibcalls itself two times with smaller values ofn, i.e.Fibcalls itself recursively.

Thus, the recursive Fibonacci algorithm is a direct implementation of the mathematical definition for the Fibonacci numbers. Thebase caseof the recursion is whenn=1orn=0, in which caseFibreturns1and, most importantly,does notcall itself anymore (otherwise, the recursion would never stop).

Therecursion treefor the computation of Fibonacci’s number withn=4with the algorithm above is given by Figure 1.

The root (at the top of the tree!) denotes the initial call of theFib-function, the downward arcs denote theFib-function calling itself with the corresponding values ofn. The boxesbelowthe function name denote base cases, the boxes to the right with equality sign denote the return values.

a) Using pen and paper, create therecursion treeforFib(6).

1. Which value doesnhave in each recursive call; what is the value ofFib(n)?

2. Which are the base cases?

3. In which order is the tree traversed?

4. How often doesFibcall itself?

1

(2)

Fib(4)= 5

Fib(3)= 3 Fib(2)= 2

Fib(2)= 2 Fib(1) 1

Fib(1) 1

Fib(0) 1

Fib(1) 1

Fib(0) 1

+

+ +

+

Figure 1:Recursion Tree for Fib(4)

b) Implement theFibalgorithm in C. Why is this algorithm a bad way to compute the Fibonacci numbers from an algorithmic point of view?

c) If you haven’t done so already, solveExersise 3.2.

Exercise 4.2C++ Classes – BiggerInt

In the last lecture, you saw the implementation of a class for representing larger integers,BiggerInt, with an implemen- tation of theBiggerInt *addclass member function.

a) Obtain the fileBiggerInt.ccfrom the course webpage. Compile and run it. Browse the source code and try to understand what happens. The comments should help you. If something still is unclear, don’t hesitate to ask.

Check that everything works as expected.

b) Implement the remaining class member functionsBiggerInt *sub,BiggerInt *mul, andBiggerInt *div.

c) **Replaceunsigned long low_bits;bystd::vector<unsigned int> low_bits;to represent arbitrarily large integers. The vector should dynamically adapt its size to hold any integer. Change the class member functions BiggerInt,BiggerInt *sub,BiggerInt *mul, andBiggerInt *divaccordingly.

d) **Adapt the recursive Fibonacci algorithm fromExercise 4.1for your newBiggerIntclass and test it with some large numbers. Implement an iterative version of the Fibonacci algorithm. What do you observe?

2

Referenzen

ÄHNLICHE DOKUMENTE

a trajectory in the love space (see Figure 4) representing the predicted time evolution of the involvement of Scarlett and Rhett, eleven chronologically ordered short segments of

He assumes a quite general dependence of the tran- sition probability on a physical parameter Q and, after in- troducing a new stochastic variable y, he was able to show that in

We shall now formulate two auxiliary results which will be used in the proof of Theorem 3.. Stochastic Models of Control and Economic

Notably, Pax6 is present in all vertebrates surveyed to date, whereas Pax4 and -10 were lost multiple times in independent vertebrate lineages, likely because of their

• Overlapping between the two wings of the MC (neo new and classic) renders distinguishing between them practically impossible. For it is well-known that many public and

The purpose of this paper is to study different notions of Sobolev capacity commonly used in the analysis of obstacle- and Signorini-type variational inequalities.. We review

Reissig: Weakly Hyperbolic Equations — A Modern Field in the Theory of Hyperbolic Equations, Partial Differential and Integral Equations.. International Society for

Especially with re- gards to the stability of the people’s position in the middle of society, empirical social science studies challenge the middle class narrative and