Iteration and Recursion Tonga Institute of Higher Education.

24
Iteration and Recursion Tonga Institute of Higher Education

Transcript of Iteration and Recursion Tonga Institute of Higher Education.

Page 1: Iteration and Recursion Tonga Institute of Higher Education.

Iteration and Recursion

Tonga Institute of Higher Education

Page 2: Iteration and Recursion Tonga Institute of Higher Education.

Iteration

Iteration - Repeating a set of steps. Most programs contain loops where the

body of the loop is executed over and over again.

The computer iterates (repeats) through the loop, which means that it repeatedly executes the loop.

Page 3: Iteration and Recursion Tonga Institute of Higher Education.

Simple Example of Iteration

Name: SioneSalary: 400

Name: SemisiSalary: 430

Name: AnaSalary: 300

Name: MeleSalary: 410

Name: PoliSalary: 900

We have an array that stores employee objects for the employees in your company.

We want to give each employee a 40 pa’anga raise.

We could try to find each employee in the array and give them a raise but that takes a long time.

It is better to iterate through each employee and give each one a raise.

First add40 here

Then add 40 here

Then add40 here

Then add40 here

Finally, add40 here

Page 4: Iteration and Recursion Tonga Institute of Higher Education.

Triangular Numbers Pythagoras was a famous mathematician in

ancient Greece. He made the Pythagorean theorem.

He had some people who worked under him who called themselves the Pythagorians.

The believed that this series of numbers was magical:

1, 3, 6, 10, 15, 21, 28 …

Can you see the pattern in these numbers?

Page 5: Iteration and Recursion Tonga Institute of Higher Education.

The Pattern of Triangular Numbers

Number Calculation Triangle Number

1 By Definition 1

2 1 + 2 3

3 1 + 2 + 3 6

4 1 + 2 + 3 + 4 10

5 1 + 2 + 3 + 4 + 5 15

6 1 + 2 + 3 + 4 + 5 + 6 21

7 1 + 2 + 3 + 4 + 5 + 6 + 7 28

Page 6: Iteration and Recursion Tonga Institute of Higher Education.

Why are they “Triangular” Numbers? They can be seen as a triangular arrangement of

objects, shown as little squares below.

Page 7: Iteration and Recursion Tonga Institute of Higher Education.

Triangular Number Calculation with Iteration – 1

To find 4th Triangular Number can add the number of squares in each column.

Subtract one everytime we add to thetotal

Page 8: Iteration and Recursion Tonga Institute of Higher Education.

Triangular Number Calculation with Iteration – 2

Subtract one everytime we add to thetotal

Change this to be the TriangularNumber you want to find

Used to calculate and store the Triangular Number

Page 9: Iteration and Recursion Tonga Institute of Higher Education.

Demonstration

Triangular NumbersIterationProject: TriangularNumber

Page 10: Iteration and Recursion Tonga Institute of Higher Education.

Recursion

Recursion - A programming method in which a method calls itself.

Recursion is an extremely powerful concept, but it can strain a computer's memory resources.

Recursive algorithms follow a divide-and-conquer approach.

1. Divide the problem into a number of subproblems. 2. Conquer the subproblems. If the subproblem is too hard, divide

it again until it is small enough to solve easily. 3. Combine the solutions to the subproblems into a solution for the

original problem. 

Page 11: Iteration and Recursion Tonga Institute of Higher Education.

Triangular Number Calculation with Recursion – 1

To find 4th Triangular Number can add:

1. The tallest column, which has a value of 4.

2. The sum of all the remaining columns, which is 6.

Page 12: Iteration and Recursion Tonga Institute of Higher Education.

Triangular Number Calculation with Recursion – 2

To find 4th Triangular Number can add:1. The tallest column.

2. The sum of all the remaining columns.

Adds the tallest column toThe sum of the columns

Adds the tallest column toThe sum of the remaining columns

The sumAllColumns method doesthe same thing as triangle. Soreplace sumAllColumns with triangle

1

2

3

Page 13: Iteration and Recursion Tonga Institute of Higher Education.

Triangular Number Calculation with Recursion – 3

This approach works like this: Someone tells me to find the 4th Triangular number. I know: (4th Triangular number) = (3rd Triangular number) + 4. So I tell Sione to find the 3rd Triangular number. Sione knows: (3rd Triangular number) = (2nd Triangular number) + 3 So Sione tells Ana to find the 2nd Triangular number. Ana knows: (2nd Triangular number) = (1st Triangular number) + 2 So Ana tells Taniela to get the 1st Triangular number. Taniela knows that the 1st Triangular number so he tells Ana 1 Ana tells Sione the 2nd Triangular number is 1 + 2 = 3 Sione tells me that the 3rd Triangular number is 3 + 3 = 6 So I know that the 4th Triangular number is 6 + 4 = 10

At some point, we stop asking others and provide an answer. If we never provide an answer, we will be asking questions forever.

Page 14: Iteration and Recursion Tonga Institute of Higher Education.

Triangular Number Calculation with Recursion – 4 At some point, we stop asking others and

provide an answer.

We stop asking here andgive an answer

3

4

Previously coded step

Page 15: Iteration and Recursion Tonga Institute of Higher Education.

Triangular Number Calculation with Recursion – 5

We stop asking here andgive an answer

We call this method overand over again

Change this to be the TriangularNumber you want to find

Begins the recursiveprocess

Page 16: Iteration and Recursion Tonga Institute of Higher Education.

Demonstration

Triangular NumbersRecursionProject: TriangularNumber

Page 17: Iteration and Recursion Tonga Institute of Higher Education.

Demonstration

Triangular NumbersRecursion w/ OutputsProject: TriangularNumber

Page 18: Iteration and Recursion Tonga Institute of Higher Education.

Characteristics of Recursive Methods It calls itself When it calls itself, it does so to solve a

smaller problem At some point, the problem is simple

enough so the method can return something without calling itself.

Page 19: Iteration and Recursion Tonga Institute of Higher Education.

Is Recursion Efficient?

Recursion is not as efficient as iterationMethods must be called over and over againAll the variables in all the methods must be

stored while methods calls are repeated

Page 20: Iteration and Recursion Tonga Institute of Higher Education.

Why Use Recursion?

Recursion simplifies problems conceptually.

Page 21: Iteration and Recursion Tonga Institute of Higher Education.

Factorial Calculation with Recursion - 1

Number Calculation Factorial

1 By Definition 1

2 1 * 2 2

3 1 * 2 * 3 6

4 1 * 2 * 3 * 4 24

5 1 * 2 * 3 * 4 * 5 120

6 1 * 2 * 3 * 4 * 5 * 6 720

7 1 * 2 * 3 * 4 * 5 * 6 * 7 5040

Page 22: Iteration and Recursion Tonga Institute of Higher Education.

Class Competition

Develop the program to find a factorial using the triangular number program.

Page 23: Iteration and Recursion Tonga Institute of Higher Education.

Demonstration

Factorials

Project: Factorial

Page 24: Iteration and Recursion Tonga Institute of Higher Education.

Factorial Calculation with Recursion - 2

Multiply instead of Add

Change all words that saytriangular to factorial