

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


A function is a rule that applies to a number and produces a number. If a function f is applicable to a number x then we say that f is defined on x. The result of applying f to x is called the value of f at x and denoted by f (x). If the numbers that f is defined on are arbitrary positive integers then we can think of f as an infinite sequence of values f (1), f (2),.. .. A recursive definition of a function f shows how to compute f (x) if we know the values of f at some points other than x. For example, a function f defined on all positive integers can be characterized by two equations: one for f (1), and the other defining f (n + 1) in terms of f (n). Here is a recursive definition of the sequence B from Problem Set 1:
B 1 = 1, Bn+1 = Bn + n + 1.
There are two ways to find B 5 using this definition. One is to find first B 2 , B 3 and B 4 : B 2 = B 1 + 2 = 1 + 2 = 3, B 3 = B 2 + 3 = 3 + 3 = 6, B 4 = B 3 + 4 = 6 + 4 = 10, B 5 = B 4 + 5 = 10 + 5 = 15.
The other possibility is to form a chain of equalities that begins with B 5 and ends with a number:
B 5 = B 4 + 5 = B 3 + 4 + 5 = B 3 + 9 = B 2 + 3 + 9 = B 2 + 12 = B 1 + 2 + 12 = B 1 + 14 = 1 + 14 = 15.
3.1. Here is a recursive definition of the factorial function:
0! = 1, (n + 1)! = n! · (n + 1).
Show how to calculate 5! using each of the two methods above.
3.2. Consider the function defined by the formulas
f (0) = 0, f (n + 1) = f (n) + (^2) n^1 +.
Find f (1) and f (2). Define f using sigma-notation, instead of recursion.
3.3. For any nonnegative integer n, let f (n) be the product of all odd numbers from 1 to 2n + 1:
f (n) = 1 · 3 · 5 ·... · (2n + 1).
Give a recursive definition of this function, and check that your definition gives the correct value for f (3).
3.4. Express the function from the previous problem in terms of factorials. Check that your formula gives the correct value for f (3).
3.5. Consider the function defined by the formulas
f (0) = 0, f (n + 1) = 2f (n) + 1.
Find f (n) for n = 1,... , 4. Guess what a formula for f (n) might be. Prove that this formula is correct.
3.6. Consider the function defined by the formulas
f (0) = 1, f (n + 1) = 2f (n) + n.
Find f (n) for n = 1,... , 4. Guess which values of n satisfy the condition f (n) > 2 n. Prove your conjecture.
Recursive definitions can be written in a different format, as a set of cases instead of a set of equations. For instance, the recursive definition of factorial can be rewritten as
n! =
{ 1 , if n = 0, (n − 1)! · n, otherwise. Recursive definitions in the case format can be easily translated into Java, for instance:
public static int fact(int n){ if (n==0) {return 1;} else return fact(n-1)*n; }