solmidt06 - October 27 2006 Malek Mouhoub CS 340 Midterm...

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

View Full Document Right Arrow Icon
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
Background image of page 1

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

View Full DocumentRight Arrow Icon
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
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 3

solmidt06 - October 27 2006 Malek Mouhoub CS 340 Midterm...

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

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