1 / 17

COP 3503 - Computer Science II (Fall 2005) Week Three

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.

elita
Download Presentation

COP 3503 - Computer Science II (Fall 2005) Week Three

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. COP 3503 - Computer Science II (Fall 2005)Week Three By Yunjun Zhang

  2. 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

  3. 7.8 Answer On board

  4. 7.9 Prove the following identities relating to the Fibonacci numbers F1+F2+F3+…+FN=FN+2-1

  5. 7.9 Answer On board

  6. 7.11 Prove that if A>=B, then A mod B < A/2

  7. 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

  8. 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++;

  9. 5.16 Answer on board

  10. 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)?

  11. 5.17 Answer • f(n)=f(n-1)f(n-1) f(1)=2 f(n)=pow(2,pow(2,n)); b. On board

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

  13. 1. Answer N(N+1)/2 times, N=length

  14. Ex1. Evaluate the running time for (x=2;x<=2N;x*=x) for(y=x;y>=1;y=y/2) sum=sum+1;

  15. Ex1. Answer O(n) Details on board

  16. 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;

  17. Ex2. Answer

More Related