MIS 24053 Lecture Notes - Lecture 19: Recursive Definition, Triangular Number, Fibonacci

24 views9 pages

Document Summary

If each person shakes hands once with every other person. In some problems, it may be natural to define the problem in terms of the problem itself. Recursion is useful for problems that can be represented by a simpler version of the same problem. In general, we can express the factorial function as. The factorial function is only defined for positive integers. So we should be a bit more precise: (if n is equal to 1) n! = n * (n-1)! (if n is larger than 1) factorial function. The c++ equivalent of this definition: int fac(int numb){ if(numb<=1) return 1; else return numb * fac(numb-1); recursion means that a function calls itself factorial function. Assume the number typed is 3, that is, numb=3. fac(3) : factorial function. For certain problems (such as the factorial function), a recursive solution often leads to short and elegant code. Compare the recursive solution with the iterative solution:

Get access

Grade+
$40 USD/m
Billed monthly
Grade+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
10 Verified Answers
Class+
$30 USD/m
Billed monthly
Class+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
7 Verified Answers

Related Documents