lect12_Recursion

# lect12_Recursion - Recursion Recursion CSIS1117 Computer...

This preview shows pages 1–5. Sign up to view the full content.

Recursion SIS1117 omputer Programming CS S Co pute og a g 1 c1117 lecture 12

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
ecursion Recursion uppose we want to compute the exponentiation Suppose we want to compute the exponentiation of a positive number y , i.e. y n = y y where > = 0 y , where n > 0 . When solving a problem , one basic technique is to reak e roblem to b roblems break the problem into sub-problems . Sometimes, it turns out that at least one of the sub-problems is a smaller example of the same problem. Accomplishing the sub-problems can help to solve the original problem. c1117 lecture 12 2
ecursion Recursion alculating the exponentiation of Calculating the exponentiation of y : If n equals to 0 , y 0 = 1 quals to 1 y 0 If n equals to 1 , y = y y If n equals to 2 , y 2 = y y 1 quals to y - If n equals to k , y k = y y k 1 Finishing the sub-problem y k-1 can help to solve the riginal problem k original problem y . We can use recursion to solve this kind of problem. Recursion is a function that calls itself. It is a very powerful problem solving technique. c1117 lecture 12 3

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
ecursion Recursion he core idea is to identify the cursive structure The core idea is to identify the recursive structure of a problem. nce the cursive structure known the Once the recursive structure is known, the corresponding program is usually very simple. general rule for defining a cursive function A general rule for defining a recursive function : Base case: Find out the simplest case(s) of the problem g quals to 1 E.g. n equals to 0 , y = 1 Recursive case: Find the rule for solving the problem if e sub- roblem(s) is solved the sub problem(s) is solved.
This is the end of the preview. Sign up to access the rest of the document.

## This document was uploaded on 05/04/2011.

### Page1 / 21

lect12_Recursion - Recursion Recursion CSIS1117 Computer...

This preview shows document pages 1 - 5. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online