Recursive Algorithms in Problem Solving
Recursive Algorithms in Problem Solving
The algorithms
recurring
I- Introduction
II- Calculation of sum
III- Recurrent algorithm on chains
IV- Pascal's triangle
V- The Fibonacci sequence
VI- The golden ratio
Let's remember
Exercises
Lecture
4
The recursive algorithms
I. Introduction
Activity 1
1- The calculation of the sum of the elements of a matrix requires initialization to zero of
the variable S containing the sum; we add to it the first element of the
matrix, ( S← S + M[1, 1]), we obtain a second result S to which we add
the second element of the matrix (S← S + M[1, 2]) and so on. It is the accumulation of
This PDF document was edited using Icecream PDF Editor.
Upgrade to the PRO version to remove the watermark. 124
all the elements of the matrix in the variable S.
Since this treatment always refers to the previous element, therefore it is a
first-order recurrent treatment.
Result: SUM_MAT
Processing: To calculate the sum of the elements of the matrix M of type MAT, we
we must sum all the integers it contains:
[S← 0]
Line up from 1 to NMake
Column from 1 to NMake
S← S + M [line, column]
U0→ 0
U1→ 01
U2→ 0110
U3→ i
U4→ k6
Questions:
What is the recurrence order of this sequence?
2- Propose an analysis, then deduce the algorithms from the problem
allowing to calculate and display N th term of the sequence of
Thue-Morse starting from a character A"0" or"1" ) given.
3- Translate and test the solution to the problem. Save your
program under the name Thue_Morse.
The first result depends on the first value of the character ('0' or '1'), a second
the result is obtained from the previous one found, and so on. We conclude that it is
a recurrent sequence of order 1.
2) Problem analysis
Résultat :Thue_Morse
Treatment :[CH← A]
From 1 to NDo
j← 1
Repeat
SiCH[j] = "0"Alorsinsère ("1", CH, j+1)
Insert ("0", CH, j+1)
End If
L← Length (CH)
j← j + 2
Until j > L
End For
4
Pour from 1 to N
j←1
Repeat
SiCH[j] = "0"Alorsinsère ("1", CH, j+1)
Insert ('0', CH, j+1)
End If
L← Length (CH)
j←j+2
Until j > L
End For
2) Thue_Morse← CH
3) End Thue_Morse
Local Objects Coding Table
Objects Type / Nature Role
j, i Unsigned integer Meters
CH Chain String representing the Thue-Morse sequence
PROGRAMSuite_Thue_Morse
USESCrt ;
VARN : Word ;
A : Char ;
Repeat
Give a character 0 or 1:
readLn (A);
UntilA In ['0', '1'] ;
End;
{ Programme principal }
BEGIN
Input (N, A);
WriteLn('The Thue-Morse sequence starting from ', A, ' Is ', )
Thue_Morse (N, A) ) ;
END .
4
Questions:
1- Is this treatment recurrent? If so, provide its rank.
2- Propose an analysis of the problem using an iterative process.
3- Deduce the corresponding algorithms.
4- Propose an analysis of the problem using a recursive process.
5- Deduce the corresponding algorithms.
1) Example:
For n = 3, the displayed Pascal's Triangle is as follows:
Line 1 1
Line 2 1 1
Line 3 121
Columns
1 2 3
MAT[3,2]=MAT[2,2]+MAT[2,1]
Line 1 1
Line 2 1 1
Line 3 1 2 1
Line 4 1 3 3 1
Line 5 1 4 6 4 1
Columns
1 2 3 4 5
MAT[5,4]=MAT[4,4]+MAT[4,3]
We note that the calculation of the content of cell (5,4) refers to the content of
two cells (4,4) and (4,3). It is a recurring treatment of order 2.
2) Iterative solution:
a) Analysis of the main program
Result: Display of a matrix containing the different values of Pascal's Triangle.
executed by the Display_Triangle procedure
This PDF document was edited using Icecream PDF Editor.
Upgrade to PRO version to remove the watermark. 130
Processing: Filling the Pascal's Triangle, represented by a square matrix
of order n. It is the task of the procedure Fill_MAT.
Data: an integer N that represents the number of lines of the Triangle, we use
the procedure Enter.
0) Start Pascal_Triangle
1) Proc Enter (N)
2) Proc Fill_MAT (N, MAT)
3) Proc Display_Triangle (N, MAT)
4) End Triangle_Pascal
Coding table for new types:
Type
Matrix = Array of maximum rows and maximum columns of integers
4
d/ Algorithm of the Fill_MAT procedure
0) Start procedure Fill_MAT (N: Integer; VAR MAT: Matrix);
1) MAT [1,1]← 1
2) MAT [2,1]← 1
3) MAT [2,2]← 1
4) Highlight from 3 to N
MAT [line, 1]← 1
MAT [line, line]← 1
For column 2 to line -1 Do
MAT [row, column]← MAT [row-1, column] + MAT [row-1, column-1]
End for
End for
5) Fill out_MAT
Translation in Pascal
PROGRAM Triangle_Pascal ;
USESCrt ;
constMax = 20;
VARN : Word ;
MAT : Matrix ;
{ Programme principal }
BEGIN
Enter (N);
Fill_MAT(N, MAT);
Display_Triangle (N, MAT);
END.
3/ Recursive solution:
0) Start Pascal_Triangle
1) Proc Enter (N)
2) Procedure Fill_Triangle (N, MAT)
3) Proc Display_Triangle (N, MAT)
Fin Triangle_Pascal
The coding table of new types and the coding table of objects
global ones are the same as those of the iterative solution except that the name of the
procedure Fill_TRIANGLE has become Fill_Triangle.
This PDF document was edited using Icecream PDF Editor.
Upgrade to the PRO version to remove the watermark. 134
c) Analysis of the Fill_Triangle procedure
4
The analyses and algorithms of the procedures Input and Display_Triangle are the
the same as those of the iterative solution.
The Fibonacci sequence is a recurrent sequence where each element obeys the relation of
next recurrence: U = U n+ U n-1 n-2
withU = 1 1 and U 2= 1
So it is a second-order recurrent sequence.
Activity 4
4
Algorithm
0) Start function FIBO_IT1 (N: Integer): Integer
u1← 1, u2← 1
If N ≤ 2 Then F← 1
Otherwise
Pouri from 3 to NMake
F← u1 + u2
u1← u2
u2← F
End For
End If
2) FIBO_IT1← F
3) End FIBO_IT1
Algorithm
0) Start function FIBO_IT2 (N : Integer ; T : Array) : Integer
T[1]← 1, T[2]← 1
If Sin ≤ 2 Then F← 1
Otherwise
Pouri from 3 to NDo
T[i]← T[i-1] + T[i-2]
End For
F← T[n]
End If
2) FIBO_IT2← F
3) End FIBO_IT2
Algorithm
0) Start function FIBO_REC (N : Integer) : Integer
1) If N ≤ 2 Then FIBO_REC← 1
SinonFIBO_REC← FN FIBO_REC (N-1) + FN FIBO_REC (N-2)
End Yes
2) End FIBO_REC
Activity 5
Complétez le tableau suivant par Fib (n+1) / Fib (n) et par sa valeur
approached. What do you deduce?
Approximate value of
n Fib (n) Fib (n+1) / Fib (n)
Fib (n+1) / Fib (n)
1 1 1 1
2 1 2 2
3 2 3/2 1.5
4 3 5/3 1,666
5 5 8/5 1.6
6 8 13/8 1,625
7 13 21/13 1,615
8 21
9 34
10 55
11 89
12 144
13 233
The golden ratio is usually designated by the letterλ (phi) of the Greek alphabet
in honor of Phidias, sculptor and architect of the Parthenon (see reading section).
PROGRAMName_Or ;
USESCrt ;
VARU: TAB_E;
V: TAB_R ;
i: Integer;
{Programme principal }
BEGIN
Init (U) ;
Calculation (U, V, i);
Display (V, i) ;
END.
Exercise 2
Write a program that calculates and displays the sum of the squares of the first n
odd integers.
For example, for n = 5
The program displays:
n = 5, the series is: 1 + 23 + 52+ 7 +
2 9 =2165. 2
Write a program that displays an isosceles triangle made of stars with N lines (N
is provided to the keyboard):
*
***
*****
*******
*********
***********
*************
***************
Exercise 4
U0 = x
Un+1 (U / 2)
n + (x / 2U) n
Exercise 6
We remind you that the function Random (i: Integer) returns a random integer.
ranging from 0 to i-1. The call to RANDOMIZE allows to initialize this
function.
PROCEDUREX
Begin
Randomize ; {initialization of the Random function}
For i := 1 TO 20001 DO T[i] := 1 + Random(20000);
End;
{Programme principal}
BEGIN
X;
i:=1; coincide:=false;
Repeat
i := i + 1; S:=0 ;
While (S < i - 1) AND NOT (coincide) Do
Begin
S:=S+1;
If T[S]=T[i] Then coincide:=TRUE
End;
Untilcoincide=true;
U:= i ;
For n:=1 to i Do
Write (T[n] , ' ; ');
WriteLn ;
WriteLn ('U = ', U);
Writeln ('S = ', S);
Readln;
END.
U0 = 1, n+1
U (U +nV) / 2n
V0 = 2, n+1
V = √ Un+1 * nV
This PDF document was edited using Icecream PDF Editor.
Upgrade to the PRO version to remove the watermark. 146
We admit that the sequences (U) and
n (V) are
n adjacent with a limit of √27/π.
Write a program that reads an integer n and displays the approximation of the number π obtained from
starting from
n V.
Exercise 8
Calculate the th
N term U of
n the Fibonacci sequence which is given by the
the following recurrence relation:
U1= 1
U2= 1
Un= Un-1 + Un-2 (for N>2)
Determine the rank N and the value U
n of the maximum term that can be calculated if
we use for U: n
the integer type
the long integer type.
Exercise 9: Vieta's Formula
Let there be two recurrent sequences u and v:
V0 = 0 Vn+1 =
Un
and U
0 =2 Un+1 =
Vn + 1
Check this assertion with a program.
Exercise 10: Sequence converging towards the square root of N
Let the sequence be defined by U0 = N/2 and
n+1U(U +nN / U) / 2,
n where N is an integer
natural
Assuming its convergence, verify that its limit is the square root of N.
- Calculate its successive terms as long as the absolute value of the difference between the two
-6 is | U
consecutive terms exceed 10, that -6
n+1 - Un| > 10
Display the number of iterations performed.
Leonardo Pisano
Fibonacci.
Leonardo Fibonacci (Pisa, c. 1170 - c. 1250) is a
Italian mathematician. Fibonacci (by his modern name),
known at the time as Leonardo Pisano
(Leonardo of Pisa), but also of Leonardo Bigollo
(bigollo meaning traveler), was actually called
Leonardo Guilielmi.
Biography
Born in Pisa, Italy, his education was largely completed in
North African party. His father, Guilielmo Bonacci,
managed the markets of the Republic of Pisa in Algeria, in
Tunisia and Morocco. In 1202, he brought back the figures.
Arabs and algebraic notation (which some attribute)
the introduction to Gerbert of Aurillac.
In 1202, he published Liber Abaci ('The Book of Calculations'), a treatise on calculations and the
accounting based on decimal calculation at a time when all of the West was still using the
Roman numerals and calculation on an abacus. This book is heavily influenced by his life in foreign countries.
Arabic; it is also written partly from right to left.
With this publication, Fibonacci introduced the Indian numeral system to Europe. This system
is much more powerful and faster than Roman notation, and Fibonacci is fully aware of it.
However, it struggles to assert itself for several centuries. The invention will be poorly received because the public does not
understood more the calculations that merchants were making. In 1280, Florence even prohibited
the use of Arabic numerals by bankers. It was judged that the 0 brought confusion and
difficulties to the point that they called this system cifra, which means 'secret code'.
Fibonacci is known today for a problem leading to numbers and the sequence that
as its name suggests, but in its time, it was mainly the applications of arithmetic to calculation
commercials that made it recognized: transaction profit calculation, currency conversion
from different countries. His work on number theory was ignored during his lifetime. Later, some
serious studies conducted about him led to esoteric uses, which can even be found
at the level of certain stock market methods (technical analysis). The name of Fibonacci,
corresponding to the "son of Bonacci", was posthumously attributed to him.
The story
10,000 years ago: First human manifestation of knowledge of the golden ratio (temple
d'Andros discovered underwater in the Bahamas.
2800 BC: The pyramid of Khufu has dimensions that highlight its importance.
what are architects attached to the golden ratio.
Vth century BC (447-432 BC): The Greek sculptor Phidias uses the golden ratio to
decorate the Parthenon in Athens, especially to sculpt the statue of Athena Parthenos. He used
is also the square root of 5 as a ratio.
IIIème century BC: Euclid mentions the division of a segment into "extreme and mean ratio"
in Book VI of the Elements.
1498: Fra Luca Pacioli, a monk professor of mathematics, writes De divina proportione ("The Divine Proportion")
divine proportion.
In the 19thth century: Adolf Zeising (1810-1876), doctor of philosophy and professor in Leipzig
then Munich, speaks of 'golden section' (der goldene Schnitt) and is interested in it not only in terms of geo-
but when it comes to aesthetics and architecture. He seeks this relationship, and finds it (one
easily find what one is looking for ...) in many classical monuments. It is he who intro-
it says the mythical and mystical side of the golden number.
th
At the beginning of the 20th
century: Matila Ghyka, Romanian diplomat, relies on the works of the phi-
German philosopher Zeising and German physicist Gustav Theodor Fechner; his works
The Aesthetics of Proportions in Nature and Arts (1927) and The Golden Ratio. Rites and Rhythms
my Pythagoreans in the development of Western civilization (1931) emphasize the pre-
eminence of the golden number and definitively establish the myth.
During the 20th th century: painters such as Dali and Picasso, as well as architects like Le
Corbusier resorted to the golden ratio.
1945: Le Corbusier patents his Modulor which provides a system of proportions between the dif-
referenced parts of the human body.