lect12_Recursion

lect12_Recursion - Recursion Recursion CSIS1117 Computer...

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

View Full Document Right Arrow Icon
Recursion SIS1117 omputer Programming CS S Co pute og a g 1 c1117 lecture 12
Background image of page 1

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

View Full DocumentRight Arrow Icon
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
Background image of page 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
Background image of page 3

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

View Full DocumentRight Arrow Icon
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.
Background image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

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 Right Arrow Icon
Ask a homework question - tutors are online