Professional Documents
Culture Documents
Recursion: - Definition of Factorial: ! 1 2 2 1 - Recursive Definition: !, 0 1, 0
Recursion: - Definition of Factorial: ! 1 2 2 1 - Recursive Definition: !, 0 1, 0
Recursion: - Definition of Factorial: ! 1 2 2 1 - Recursive Definition: !, 0 1, 0
Factorial Factorial
// Pre: n >= 0 • Definition of factorial:
// Returns n!
Introduction to Programming © Dept. CS, UPC 3 Introduction to Programming © Dept. CS, UPC 4
Factorial Recursive design
In the design of a recursive program, we usually follow a
// Pre: n >= 0 sequence of steps:
// Returns n!
1. Identify the basic cases (those in which the subprogram can
int factorial(int n) { // recursive solution solve the problem directly without recurring to recursive
calls) and determine how they are solved.
if (n == 0) return 1;
else return nfactorial(n - 1); For example, in the case of factorial, the only basic case used
} in the function is n=0. Similarly, we could have considered a
more general basic case (e.g., n ≤ 1). In both cases, the
function should return 1.
Introduction to Programming © Dept. CS, UPC 5 Introduction to Programming © Dept. CS, UPC 6
3. Make sure that the parameters of the call move closer to the int Collatz(int n) { // recursive solution
basic cases at each recursive call. This should guarantee a if (n == 1) return 0;
finite sequence of recursive calls that always terminates. else if (n%2 == 0) return 1 + Collatz(n/2);
else return 1 + Collatz(3n + 1);
In the case of a factorial, n-1 is closer to 0 than n. Therefore, }
we can guarantee that this function terminates.
Introduction to Programming © Dept. CS, UPC 7 Introduction to Programming © Dept. CS, UPC 8
Recursion: behind the scenes Recursion: behind the scenes
... ...
f = factorial(4); f = factorial(4);
24
... ...
int factorial(int n)
4 24 factorial(int n)
int 4
if (n4 <= 1) return 1; if (n4 <= 1) return 1;
else return n4 factorial(n-1);
3 else return n4 factorial(n-1);
24 6 3
int factorial(int n)
3 6 factorial(int n)
int 3
if (n3 <= 1) return 1; if (n3 <= 1) return 1;
else return n3 factorial(n-1);
2 else return n3 factorial(n-1);
6 2 2
int factorial(int n)
2 2 factorial(int n)
int 2
if (n2 <= 1) return 1; if (n2 <= 1) return 1;
else return n2 factorial(n-1);
1 else return n2 factorial(n-1);
2 1 1
int factorial(int n)
1 1 factorial(int n)
int 1
if (n1 <= 1) return 1; if (n1 <= 1) return 1;
else return n1 factorial(n-1); else return n1 factorial(n-1);
Introduction to Programming © Dept. CS, UPC 9 Introduction to Programming © Dept. CS, UPC 10
Introduction to Programming © Dept. CS, UPC 13 Introduction to Programming © Dept. CS, UPC 14
• We can assume that the text always has at least one • General case:
character (the last ‘.’) We have something different from ‘.’ at the input calculate the number of
remaining ‘a’ at the input and add 1 if the current char is an ‘a’
• Example: the text
Programming in C++ is amazingly easy !.
has 4 a’s
Introduction to Programming © Dept. CS, UPC 21 Introduction to Programming © Dept. CS, UPC 22
Introduction to Programming © Dept. CS, UPC 23 Introduction to Programming © Dept. CS, UPC 24
Tower of Hanoi Tower of Hanoi
Introduction to Programming © Dept. CS, UPC 25 Introduction to Programming © Dept. CS, UPC 26
Introduction to Programming © Dept. CS, UPC 29 Introduction to Programming © Dept. CS, UPC 30
Introduction to Programming © Dept. CS, UPC 31 Introduction to Programming © Dept. CS, UPC 32
Tower of Hanoi Tower of Hanoi
Hanoi(0,L,M,R)
• How many moves do we need for n disks?
Hanoi(1,L,R,M) LR
Moves(n) = 1 + 2Moves(n-1)
Hanoi(0,M,R,L)
Hanoi(2,L,M,R) LM
Hanoi(0,R,L,M) n Moves(n)
Hanoi(1,R,M,L) RM 1 1
Hanoi(0,L,M,R)
Hanoi(3,L,R,M) LR 2 3
Hanoi(0,M,R,L)
3 7
Hanoi(1,M,L,R) ML
4 15
Hanoi(0,R,L,M)
Hanoi(2,M,R,L) MR 5 31
Hanoi(0,L,M,R)
Hanoi(1,L,R,M) LR
6 63
Hanoi(0,M,R,L)
n 2n-1
Introduction to Programming © Dept. CS, UPC 33 Introduction to Programming © Dept. CS, UPC 34
Introduction to Programming © Dept. CS, UPC 35 Introduction to Programming © Dept. CS, UPC 36
Digital root Digital root
• Basic case: n can be represented as a single- // Assume we have a function (to be defined)
digit number return n // that calculates the sum of the digits of a number
// Pre: n ≥ 0
// Returns the sum of the digits of n
• General case: n has more than one digit // (represented in base 10)
int sumdigits(int n);
– Calculate the sum of the digits
– Calculate the digital root of the sum // Pre: n ≥ 0
// Returns the digital root of n
int digital_root(int n) {
if (n < 10) return n;
else return digital_root(sumdigits(n));
}
Introduction to Programming © Dept. CS, UPC 37 Introduction to Programming © Dept. CS, UPC 38
Introduction to Programming © Dept. CS, UPC 39 Introduction to Programming © Dept. CS, UPC 40
Write a number n in base b
// Writes the representation of n in base b (n ≥ 0, 2 b 10)
// No digits are written when n = 0.
void write_base(int n, int b) {
if (n > 0) {
write_base(n/b, b);
cout << n%b;
}
}