Fl03hw1

Fl03hw1 - Complete the stopping conditions (describe as...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
HW1) [10=10+10] pts 1. Program and compare the complexity of those two algorithms. a) for (i=0; i<n; i++) { complexity++; } b) for (i=0; i<n; i++) { for (j=0; j<=i*i; j++) { for (k=0; k<j; k++) { complexity++; } } } You may pick any 4 different values for n. n Complexity of a Complexity of b 1 1 0 2 2 1 3 2 11 4 4 56 2. Show the changes of the values for each variable (i, j) by hand (n=5). for (i=1; i<n; i++) { for (j=1; j<i*i; j++) { if (j%i==0){ System.out.println(“i=” + i + “ j=” + j); } } } i j 2 2 3 3 3 6 4 4 4 8 4 12 3. A gcd algorithm is provided below based on the following observation (arrange so that a > b). gcd(a,b) = 2gcd(a/2, b/2) if a and b are both even. gcd(a,b) = gcd(a/2,b) if a is even and b is odd. gcd(a,b) = gcd(a,b/2) if a is odd and b is even. gcd(a,b) = gcd((a+b)/2, (a-b)/2) if a and b are both odd.
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Complete the stopping conditions (describe as specifically as possible) public static int gcd (int a, int b) { if (a<b) {int temp=a; a=b; b=temp;} // arrange so that a > b. if ( ? ) { return ? ; /* recursion termination */} if (b==0) return a; if (a==b) return a; if (a==b) return b; if (a%b==0) return b; {if (a==1||b==1) return 1; if (a==b) return b;} if (a%2==0 && b%2==0) // if a and b are both even return 2*gcd(a/2, b/2); else if (a%2==0 && b%2!=0) // if a is even and b is odd return gcd(a/2, b); else if (a%2!=0 && b%2==0) // if a is odd and b is even return gcd (a, b/2); else // if a and b are both odd return gcd ((a+b)/2, (a-b)/2); }...
View Full Document

This note was uploaded on 02/10/2012 for the course CSE 5211 taught by Professor Dmitra during the Spring '12 term at FIT.

Page1 / 2

Fl03hw1 - Complete the stopping conditions (describe as...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online