170 likes | 290 Views
COP 3503 - Computer Science II (Fall 2005) Week Three. By Yunjun Zhang. Ch7 Exercises. 7.8 The fibonacci numbers: F0,F1,F2,… are defined as follow: F0=0;F1=1; FN=F(N-1)+F(N-2); Prove by induction the formula. 7.8 Answer On board.
E N D
COP 3503 - Computer Science II (Fall 2005)Week Three By Yunjun Zhang
Ch7 Exercises • 7.8 The fibonacci numbers: F0,F1,F2,… are defined as follow: F0=0;F1=1; FN=F(N-1)+F(N-2); Prove by induction the formula
7.8 Answer On board
7.9 Prove the following identities relating to the Fibonacci numbers F1+F2+F3+…+FN=FN+2-1
7.9 Answer On board
7.11 Answer Proof: if B<=A/2 A mod B <A/2 because the remainder should smaller than B. otherwise, the remainder is A-B<A/2
Running Time Analysis • 5.16 Give a Big-Oh analysis of the running time for(int i=1;i<=n;i++) for(int j=1;j<=i*i;j++) if(j%i==0) for(int k=0;k<j;k++) sum++;
5.16 Answer on board
5.17 In a court case, a judge cited a city for contempt and ordered a fine of $2 for the first day. Each subsequent day, until the city followed the judge’s order, the fine was squared. ($2, $4, $16, $256, $65536…) a. What would be the fine on day N? b. How many days would it take for the fine to reach D dollars (a Big-Oh answer will do)?
5.17 Answer • f(n)=f(n-1)f(n-1) f(1)=2 f(n)=pow(2,pow(2,n)); b. On board
Analysis of “for” loops 1. For the quadratic algorithm for the maximum contiguous subsequence sum problem, determine precisely how many times the innermost statement is executed maxSubSum(a[]) { maxSum=0; for( i=0;i<a.length;i++) { thisSum=0; for(j=i;j<a.length;j++) { thisSum+=a[j]; if(thisSum>maxSum) { maxSum=thisSum; seqStart=i; seqStart=j; } } } }
1. Answer N(N+1)/2 times, N=length
Ex1. Evaluate the running time for (x=2;x<=2N;x*=x) for(y=x;y>=1;y=y/2) sum=sum+1;
Ex1. Answer O(n) Details on board
Ex2. Given the following pseudocode segment, determine the value of x when the for loops end in terms of n. x = 0; for i = n to 3*n do for j = 1 to n-2 do x = x + j;