Practice Question_ch3_Sol

advertisement
Assignment-3 Solution
Chapter 3: Induction and Recursion
1. Find f(1), f(2), f(3), and f(4) if f (n) is defined recursively
by f(0) = 1 and for n = 0, 1, 2,...
a) f(n+ 1) = f (n) + 2.
b) f(n+ 1) = 3f (n).
solution: f(n+ 1) = f (n) + 2.
F(0+1)= f(0) +2=1+2=3
F(1+1)= f(1) +2=3+2=5
F(2+1)= f(2) +2=5+2=7
F(3+1)= f(3) +2=7+2=9
b. f(n+ 1) = 3f (n).
F(0+1)=3 f(0)=3
F(1+1)=3 f(1)=9
F(2+1)=3 f(2)=27
F(3+1)=3 f(27)=81
3.Give a recursive definition of the sequence {an}, n =1, 2, 3,... if
a) an== 6n.
b) a= 2n + 1
solution: a) an== 6n.
a1=6,a2=12,a3=24……….
b) a= 2n + 1
a1=3,a2=3, a3=7…………
Prepared by sabiha begum
ics-252
chapter-3
4. Give a recursive definition of the set of positive integers
that are multiples of 5.
Solutions: s is an integer
Basic step: 5 € s
Recursive step: if x€ s, y€s then x+y €s
5+5=10
5+10=15
5+15=20
5+20=25
5. Give a recursive definition of an, where a is nonzero real number and n is a
nonnegative integer.
solution: The recursive definition contain two parts
1. If an,were n=1,2,3…………… non-negative integers
a0=1
a n+1= a n a
6. Give a recursive algorithm for computing the greatest common divisor of two
nonnegative integers a and b with a < b.
solution:
Prepared by sabiha begum
ics-252
chapter-3
7. The fibonacci numbers, f0, f1, f2……. Are defined by the equations f0 = 0, f1 =
1 and fn= fn-1 + fn-2
For n = 2, 3, 4 , …….
Find the fibonacci numbers f2, f3, f4 , f5, and f6.
solution:
8. Prove that the program segment
y := 1
z := x + y
is correct with respect to the initial assertion x = 0 and
the final assertion z = 1.
solution: x y
0 1
1 Z=0+1=1
Thus P[s]q is true final assertion
Prepared by sabiha begum
ics-252
chapter-3
9. Verify that the program segment
x := 2
z := x + y
if y>0 then
z := z + 1
else
z := 0
is correct with respect to the initial assertion y = 3 and
the final assertion z = 6.
solution: As initial assertion y = 3, then from the program segment
x=2
z=x+y=2+3=5
as y = 3 > 0 then
z = 5 + 1 = 6 , which is the final assertion.
Prepared by sabiha begum
ics-252
chapter-3
Download