solmidt06

# solmidt06 - Malek Mouhoub CS 340 Midterm Exam Duration 50...

This preview shows pages 1–3. Sign up to view the full content.

October 27 2006 Malek Mouhoub CS 340 Midterm Exam October 27 2006 Duration : 50 min - Total : 25pts Exercise 1 : Algorithm Analysis [6pts] Give an analysis of the running time (Big-Oh notation) for each of the following 4 program frag- ments. Note that the running time corresponds here to the number of times the operation sum++ is executed. sqrt is the function that returns the square root of a given number. 1. sum = 0; for(i=0;i<sqrt(n)/4;i++) sum++; for(j=i ;j<sqrt(n)/8;j++) sum++; for(k=0;k<j;k++) sum++; 2. sum = 0; for(i=0;i<sqrt(n)/2;i++) for(j=i;j<8+i;j++) for(k=j;k<j/4;k++) sum++; 3. sum = 0; for(i=1;i<2*n;i++) for(j=1;j<i*i;j++) for(k=1;k<j;k++) if (j % i == 1) sum++; 4. sum = 0; for(i=1;i<2*n;i++) for(j=1;j<i*i;j++) for(k=1;k<j;k++) if (j % i) sum++; Solution 1. O ( n ) 2. O (1) 3. O ( n 4 ) 4. O ( n 5 ) 1/2

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

View Full Document
October 27 2006 Malek Mouhoub CS 340 Midterm Exam Exercise 2 : Prime numbers [5pts] 1. Write an O ( N ) algorithm to determine if a positive integer N , is prime. 2. Let
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 3

solmidt06 - Malek Mouhoub CS 340 Midterm Exam Duration 50...

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

View Full Document
Ask a homework question - tutors are online