Upload
asher-price
View
221
Download
1
Embed Size (px)
Citation preview
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.
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
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?
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
Why are they “Triangular” Numbers? They can be seen as a triangular arrangement of
objects, shown as little squares below.
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
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
Demonstration
Triangular NumbersIterationProject: TriangularNumber
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.
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.
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
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.
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
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
Demonstration
Triangular NumbersRecursionProject: TriangularNumber
Demonstration
Triangular NumbersRecursion w/ OutputsProject: TriangularNumber
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.
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
Why Use Recursion?
Recursion simplifies problems conceptually.
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
Class Competition
Develop the program to find a factorial using the triangular number program.
Demonstration
Factorials
Project: Factorial
Factorial Calculation with Recursion - 2
Multiply instead of Add
Change all words that saytriangular to factorial