Docsity

Recursive Definitions - Logic, Sets and Functions - Problem Set 3 | C S 313K, Study notes of Mathematical logic

Toggle sidebar
bg1
CS313K: Logic, Sets and Functions
Fall 2010
Problem Set 3: Recursive Definitions
Afunction is a rule that applies to a number and produces a number. If a
function fis applicable to a number xthen we say that fis defined on x.
The result of applying fto xis called the value of fat xand denoted
by f(x).
If the numbers that fis defined on are arbitrary positive integers then
we can think of fas an infinite sequence of values f(1), f(2),....
Arecursive definition of a function fshows how to compute f(x) if we
know the values of fat 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 Bfrom Problem Set 1:
B1= 1,
Bn+1 =Bn+n+ 1.
There are two ways to find B5using this definition. One is to find first B2,
B3and B4:
B2=B1+ 2 = 1 + 2 = 3,
B3=B2+ 3 = 3 + 3 = 6,
B4=B3+ 4 = 6 + 4 = 10,
B5=B4+ 5 = 10 + 5 = 15.
The other possibility is to form a chain of equalities that begins with B5
and ends with a number:
B5=B4+ 5
=B3+ 4 + 5 = B3+ 9
=B2+ 3 + 9 = B2+ 12
=B1+ 2 + 12 = B1+ 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.
1
pf3

Partial preview of the text

Download Recursive Definitions - Logic, Sets and Functions - Problem Set 3 | C S 313K and more Study notes Mathematical logic in PDF only on Docsity!

CS313K: Logic, Sets and Functions

Fall 2010

Problem Set 3: Recursive Definitions

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; }