Recursion. Circular Definition vs Recursive Definition Circular definition oak A tree that grows...

19
Recursion

Transcript of Recursion. Circular Definition vs Recursive Definition Circular definition oak A tree that grows...

Page 1: Recursion. Circular Definition vs Recursive Definition Circular definition  oak A tree that grows from an acorn, which is a nut produced by an oak tree.

Recursion

Page 2: Recursion. Circular Definition vs Recursive Definition Circular definition  oak A tree that grows from an acorn, which is a nut produced by an oak tree.

Circular Definition vs Recursive Definition Circular definition

oak A tree that grows from an acorn, which is a nut

produced by an oak tree

Recursion See recursion

Page 3: Recursion. Circular Definition vs Recursive Definition Circular definition  oak A tree that grows from an acorn, which is a nut produced by an oak tree.

Non-circular definitionRecursion

If you don’t get it, see recursion

Has a terminating case (i.e., “if you get it.”)

Page 4: Recursion. Circular Definition vs Recursive Definition Circular definition  oak A tree that grows from an acorn, which is a nut produced by an oak tree.

Recursive Definition

Recursive DefinitionUses the term being defined as part of the

definition Is not a circular definition

Factorial of n (n!)

n! = 1, when n = 0 // base casen! = n (n – 1)!, when n > 0

Page 5: Recursion. Circular Definition vs Recursive Definition Circular definition  oak A tree that grows from an acorn, which is a nut produced by an oak tree.

Recursive Function Recursive function

Invokes itself within the function Is an example of divide-and-conquor technique

Recursive factorial function

int fact(int n){ if (n == 0) return 1; else return n * fact(n – 1);}

Invocation

cout << fact(4);

Page 6: Recursion. Circular Definition vs Recursive Definition Circular definition  oak A tree that grows from an acorn, which is a nut produced by an oak tree.

Recursive Factorial Function

fact(4) return 4 * fact(3)

fact(3) return 3 * fact(2)

fact(2) return 2 * fact(1)

fact(1) return 1 * fact(0)

fact(0) return 1

fact(1) return 1 * 1

fact(2) return 2 * 1

fact(3) return 3 * 2

fact(4) return 4 * 3

Page 7: Recursion. Circular Definition vs Recursive Definition Circular definition  oak A tree that grows from an acorn, which is a nut produced by an oak tree.

Sum: 1 + 22 + 32 + 42 … + n2

Write a recursive function which returns the sum of squares of n integers.

Invocation

cin >> n;cout << sumSquares(n);

Page 8: Recursion. Circular Definition vs Recursive Definition Circular definition  oak A tree that grows from an acorn, which is a nut produced by an oak tree.

Sum: 1 + 22 + 32 + 42 … + n2

n = 1: sum = 1n = 2: sum = (1) + 22

n = 3 sum = (1 + 22)+ 32

n = 4: sum = (1 + 22 + 32) + 42

n = 5: sum = (1 + 22 + 32 + 42)+ 52

…n = n: sum = (1 + 22 + …(n -1)2)+ n2

Page 9: Recursion. Circular Definition vs Recursive Definition Circular definition  oak A tree that grows from an acorn, which is a nut produced by an oak tree.

Sum: 1 + 22 + 32 + 42 … + n2

long sumSquares(int n){ if (n == ) return 0; else return sumSquares(n – 1) + n * n; }

Page 10: Recursion. Circular Definition vs Recursive Definition Circular definition  oak A tree that grows from an acorn, which is a nut produced by an oak tree.

Recursive Sum of array elements

Given:

int list[] = {2, 4, 6, 8, 10};int count = 5;cout << sumArray(list, count);

Page 11: Recursion. Circular Definition vs Recursive Definition Circular definition  oak A tree that grows from an acorn, which is a nut produced by an oak tree.

Recursive Sum of array elements

n = 1: sum = list[0]n = 2: sum = (list[0]) + list[1]n = 3: sum = (list[0] + list[1]) + list[2]n = 4: sum = (list[0] + list[1] + list[2]) + list[3]. . .n = n: sum = (list[0] + list[1]… list[n – 2]) + list[n – 1]

Page 12: Recursion. Circular Definition vs Recursive Definition Circular definition  oak A tree that grows from an acorn, which is a nut produced by an oak tree.

Recursive Sum of array elements

long sumArray(int list[], int n){ if (n == 1) return list[0]; else return sumArray(list, n – 2) + list[n – 1]; }

Page 13: Recursion. Circular Definition vs Recursive Definition Circular definition  oak A tree that grows from an acorn, which is a nut produced by an oak tree.

Fibonacci Numbers

0 1 2 3 4 5 6 7 8 9 10 11 1 1 2 3 5 8 13 21 34 55 89 144

Page 14: Recursion. Circular Definition vs Recursive Definition Circular definition  oak A tree that grows from an acorn, which is a nut produced by an oak tree.

Fibonacci Numbers

fib(n) = 1, when n = 0fib(n) = 1, when n = 1fib(n) = fib(n -1) + fib(n – 2), when n > 1

Fibonacci Function

int fib(int n){ if (n == 0 || n == 1) return 1; else return fib(n – 1) + fib(n – 2);}

Page 15: Recursion. Circular Definition vs Recursive Definition Circular definition  oak A tree that grows from an acorn, which is a nut produced by an oak tree.

Golden Ratio

Greek and Renaissance Architect considered the golden ratio for a rectangle.

1

1.61803…

Page 16: Recursion. Circular Definition vs Recursive Definition Circular definition  oak A tree that grows from an acorn, which is a nut produced by an oak tree.

Golden Ratio Euclid:

"A straight line is said to have been cut in extreme and mean ratio when, as the whole line is to the greater segment, so is the greater to the less.” (Elements)

a b

a + ba a

a + 1 a 1

a b

= =φ=φ

=

Page 17: Recursion. Circular Definition vs Recursive Definition Circular definition  oak A tree that grows from an acorn, which is a nut produced by an oak tree.

Golden Ration & Fibonacci Nos.Fib Ratio Decimal 1 1/1 1.0 1 3/1 2.0 2 3/2 1.5 3 5/3 1.666. . . 5 8/5 1.6 8 13/8 1.625 13 21/13 1.615. . . 21 34/21 1.619. . . 34 55/34 1.617. . . . . . . . . . . . . . . . . . 1.6180 . . . φ

Page 18: Recursion. Circular Definition vs Recursive Definition Circular definition  oak A tree that grows from an acorn, which is a nut produced by an oak tree.

Your Turn

1. Write and test a recursive function which imports positive integer n and returns the sum of consecutive integers from 1 to n, inclusive.

2. Write and test a recursive function which returns xn.

3. Write a C++ program which generates the first n Fibonacci numbers, where n is input from the console.

Page 19: Recursion. Circular Definition vs Recursive Definition Circular definition  oak A tree that grows from an acorn, which is a nut produced by an oak tree.

Your Turn

Write and test a recursive function which imports an int array, start index, & last index and prints the list contents from position start to last.

Write and test a recursive function which imports an int array and its count and prints the list contents in reverse order.