Presentation is loading. Please wait.

Presentation is loading. Please wait.

Iteration and Recursion Tonga Institute of Higher Education.

Similar presentations


Presentation on theme: "Iteration and Recursion Tonga Institute of Higher Education."— Presentation transcript:

1 Iteration and Recursion Tonga Institute of Higher Education

2 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.

3 Simple Example of Iteration Name: Sione Salary: 400 Name: Semisi Salary: 430 Name: Ana Salary: 300 Name: Mele Salary: 410 Name: Poli Salary: 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 add 40 here Then add 40 here Then add 40 here Then add 40 here Finally, add 40 here

4 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?

5 The Pattern of Triangular Numbers NumberCalculationTriangle Number 1By Definition1 21 + 23 31 + 2 + 36 41 + 2 + 3 + 410 51 + 2 + 3 + 4 + 515 61 + 2 + 3 + 4 + 5 + 621 71 + 2 + 3 + 4 + 5 + 6 + 728

6 Why are they “Triangular” Numbers? They can be seen as a triangular arrangement of objects, shown as little squares below.

7 Triangular Number Calculation with Iteration – 1 To find 4 th Triangular Number can add the number of squares in each column. Subtract one every time we add to the total

8 Triangular Number Calculation with Iteration – 2 Subtract one every time we add to the total Change this to be the Triangular Number you want to find Used to calculate and store the Triangular Number

9 Demonstration Triangular Numbers Iteration Project: TriangularNumber

10 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.

11 Triangular Number Calculation with Recursion – 1 To find 4 th 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.

12 Triangular Number Calculation with Recursion – 2 To find 4 th Triangular Number can add: 1. The tallest column. 2. The sum of all the remaining columns. Adds the tallest column to The sum of the columns Adds the tallest column to The sum of the remaining columns The sumAllColumns method does the same thing as triangle. So replace sumAllColumns with triangle 1 2 3

13 Triangular Number Calculation with Recursion – 3 This approach works like this:  Someone tells me to find the 4 th Triangular number.  I know: (4 th Triangular number) = (3 rd Triangular number) + 4.  So I tell Sione to find the 3 rd Triangular number.  Sione knows: (3 rd Triangular number) = (2 nd Triangular number) + 3  So Sione tells Ana to find the 2 nd Triangular number.  Ana knows: (2 nd Triangular number) = (1 st Triangular number) + 2  So Ana tells Taniela to get the 1 st Triangular number.  Taniela knows that the 1 st Triangular number so he tells Ana 1  Ana tells Sione the 2 nd Triangular number is 1 + 2 = 3  Sione tells me that the 3 rd Triangular number is 3 + 3 = 6  So I know that the 4 th 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.

14 Triangular Number Calculation with Recursion – 4 At some point, we stop asking others and provide an answer. We stop asking here and give an answer 3 4 Previously coded step

15 Triangular Number Calculation with Recursion – 5 We stop asking here and give an answer We call this method over and over again Change this to be the Triangular Number you want to find Begins the recursive process

16 Demonstration Triangular Numbers Recursion Project: TriangularNumber

17 Demonstration Triangular Numbers Recursion w/ Outputs Project: TriangularNumber

18 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.

19 Is Recursion Efficient? Recursion is not as efficient as iteration  Methods must be called over and over again  All the variables in all the methods must be stored while methods calls are repeated

20 Why Use Recursion? Recursion simplifies problems conceptually.

21 Factorial Calculation with Recursion - 1 NumberCalculationFactorial 1By Definition1 21 * 22 31 * 2 * 36 41 * 2 * 3 * 424 51 * 2 * 3 * 4 * 5120 61 * 2 * 3 * 4 * 5 * 6720 71 * 2 * 3 * 4 * 5 * 6 * 75040

22 Class Competition Develop the program to find a factorial using the triangular number program.

23 Demonstration Factorials Project: Factorial

24 Factorial Calculation with Recursion - 2 Multiply instead of Add Change all words that say triangular to factorial


Download ppt "Iteration and Recursion Tonga Institute of Higher Education."

Similar presentations


Ads by Google