Download presentation
Presentation is loading. Please wait.
Published byJadyn Viles Modified over 10 years ago
1
Dr. Sharon Persinger October 30, 2013
2
Recursion is a type of repetition used in mathematics and computing to create objects and to define functions. The key to recursion is to see that some objects or functions have smaller instances of the same object or function inside them.
3
Functional recursion allows you to define a function in terms of itself. (What?) A recursive definition for a function F includes a call of the function F in the function definition for F.
4
F(n)= n(n-1)(n-2)…3 ⋅2⋅1, for integer n ≥0 F(0) = 1 Can you see a factorial on the right hand side of the definition?
5
Base case: F(0) = 1; F(1) = 1 Recursive case: for n ≥2, F(n) = n ⋅F(n-1) Trace the evaluation of F(4). In Python, an activation record keeps track of information relevant to a function call – parameter values, interruptions. Write a recursive definition in Python for the factorial function
6
Write a recursive definition for the factorial function. Now!
7
Remember Euclid’s Algorithm from CSI30? Key idea: GCD(A, B) = GCD(B, A mod B) Do example GCD(300, 45) When do you know the GCD? What is the base case? What does the recursive call look like?
8
Write a recursive definition for GCD using Euclid’s algorithm – Now!
9
There must be one or more base cases that can be evaluated without calling G There must be one or more cases where G is evaluated by calling G on a simpler or smaller input – recursive call. Every recursive call must end in a base case.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.