Você está na página 1de 20

DATA STRUCTURES : HOW THE ALGORITHM EFFECTS TIME AND SPACE COMPLEXITY

PARTHA PRATIM CHAKRAVORTY

D.S. : HOW THE ALGORITHM EFFECTS TIME AND SPACE COMPLEXITY PARTHA PRATIM CHAKRAVORTY

PROBLEM: Calculate & print prime numbers from 1 to n. Value of n will be keyed in. ALGORITHM A. Read n thru keyboard. Start from i=3 thru n. xx. For all j from 2 thru i-1 (j to be incremented by 1)divide i by j. If for this i, dividing by any j, the remainder r=0, { skip this i & make j=2 } else print i as a prime number. increase i by 1. go to xx.

D.S. : HOW THE ALGORITHM EFFECTS TIME AND SPACE COMPLEXITY PARTHA PRATIM CHAKRAVORTY

To calculate & display all the prime nos upto 1,00,000, this Program takes around 65sec in my computer. /* Prime no A.c * Author Partha Pratim Chakravorty */ /* Date : 07 Aug 2012 */ /* Prime no A.c */ #include <stdio.h> #include <conio.h> #include <math.h> int main() { int n,i,j,m1, ind; printf(" Prime no calculation A. Pl feed the upper limit n: \n"); scanf("%d",&n); printf("List of PRIME numbers :: 2");

D.S. : HOW THE ALGORITHM EFFECTS TIME AND SPACE COMPLEXITY PARTHA PRATIM CHAKRAVORTY

for(i=3; i<=n;i++) { ind =0; for(j=2; j<=i-1; j++) { m1=i/j; if (m1 * j == i ) ind= ind + 1; } if (ind==0) printf(", %d",i); } getch(); return 0; }

D.S. : HOW THE ALGORITHM EFFECTS TIME AND SPACE COMPLEXITY PARTHA PRATIM CHAKRAVORTY
/* Prime no A.c */ #include <stdio.h> #include <conio.h> #include <math.h> int main() { int n,i,j,m1, ind; printf(" Prime no calculation A. Pl feed the upper limit n: \n"); scanf("%d",&n); printf("List of PRIME numbers :: 2"); for(i=3; i<=n;i++) { ind =0; for(j=2; j<=i-1; j++) { m1=i/j; if (m1 * j == i ) ind= ind + 1; } if (ind==0) printf(", %d",i); } getch(); return 0; }

D.S. : HOW THE ALGORITHM EFFECTS TIME AND SPACE COMPLEXITY PARTHA PRATIM CHAKRAVORTY

PROBLEM: Calculate & print prime numbers from 1 to n. Value of n will be keyed in. ALGORITHM B. Read n thru keyboard. Start from i=3 thru n. xx. For all j from 2 thru i-1 (j to be incremented by 1)divide i by j. If for any j, for this i the remainder r=0, { skip this i & make j=1 } else print i as a prime number. increase i by 2. go to xx.

D.S. : HOW THE ALGORITHM EFFECTS TIME AND SPACE COMPLEXITY PARTHA PRATIM CHAKRAVORTY

To calculate & display all the prime nos upto 1,00,000, this Program takes around 35sec in my computer. /* Prime no B.c * Author Partha Pratim Chakravorty */ /* Date : 07 Aug 2012 */ #include <stdio.h> #include <conio.h> #include <math.h> int main() { int n,i,j,m1, ind; printf(" Prime no calculation B. Pl feed the upper limit n: \n"); scanf("%d",&n); printf("List of PRIME numbers :: 2");

D.S. : HOW THE ALGORITHM EFFECTS TIME AND SPACE COMPLEXITY PARTHA PRATIM CHAKRAVORTY

for(i=3; i<=n;i=i+2) { ind =0; for(j=2; j<=i-1; j++) { m1=i/j; if (m1 * j == i ) ind= ind + 1; } if (ind==0) printf(", %d",i); } getch(); return 0; }

D.S. : HOW THE ALGORITHM EFFECTS TIME AND SPACE COMPLEXITY PARTHA PRATIM CHAKRAVORTY
/* Prime no B.c */ #include <stdio.h> #include <conio.h> #include <math.h> int main() { int n,i,j,m1, ind; printf(" Prime no calculation B. Pl feed the upper limit n: \n"); scanf("%d",&n); printf("List of PRIME numbers :: 2"); for(i=3; i<=n;i=i+2) { ind =0; for(j=2; j<=i-1; j++) { m1=i/j; if (m1 * j == i ) ind= ind + 1; } if (ind==0) printf(", %d",i); } getch(); return 0; }

PROBLEM:

D.S. : HOW THE ALGORITHM EFFECTS TIME AND SPACE COMPLEXITY PARTHA PRATIM CHAKRAVORTY

Calculate & print prime numbers from 1 to n. Value of n will be keyed in. ALGORITHM C. Read n thru keyboard. Start from i=3 thru n. Calculate square root of I, add 1 and store in ii. xx. For all j from 2 thru ii (j to be incremented by 1)divide i by j. If for any j, for this i the remainder r=0, { skip this i & make j=1 } else print i as a prime number. increase i by 2. go to xx.

D.S. : HOW THE ALGORITHM EFFECTS TIME AND SPACE COMPLEXITY PARTHA PRATIM CHAKRAVORTY

To calculate & display all the prime nos upto 1,00,000, this Program takes around 3-5seconds in my computer. /* Prime no C.c * Author Partha Pratim Chakravorty */ /* Date : 07 Aug 2012 */ #include <stdio.h> #include <conio.h> #include <math.h> int main() { int n,i,j,m1,sqri, ind; printf(" Prime no calculation C. Pl feed the upper limit n: \n"); scanf("%d",&n);

D.S. : HOW THE ALGORITHM EFFECTS TIME AND SPACE COMPLEXITY PARTHA PRATIM CHAKRAVORTY

printf("List of PRIME numbers :: 2"); for(i=3; i<=n;i=i+2) { sqri=sqrt(i)+1; ind =0; for(j=2; j<=sqri; j++) { m1=i/j; if (m1 * j == i ) ind= ind + 1; } if (ind==0) printf(", %d",i); } getch(); return 0; }

D.S. : HOW THE ALGORITHM EFFECTS TIME AND SPACE COMPLEXITY PARTHA PRATIM CHAKRAVORTY
/* Prime no C.c */ #include <stdio.h> #include <conio.h> #include <math.h> int main() { int n,i,j,m1,sqri, ind; printf(" Prime no calculation C. Pl feed the upper limit n: \n"); scanf("%d",&n); printf("List of PRIME numbers :: 2"); for(i=3; i<=n;i=i+2) { sqri=sqrt(i)+1; ind =0; for(j=2; j<=sqri; j++) { m1=i/j; if (m1 * j == i ) ind= ind + 1; } if (ind==0) printf(", %d",i); } getch(); return 0; }

D.S. : HOW THE ALGORITHM EFFECTS TIME AND SPACE COMPLEXITY PARTHA PRATIM CHAKRAVORTY PROBLEM: Calculate & print prime numbers from 1 to n. Value of n will be keyed in. ALGORITHM 4. Read n thru keyboard. Start from i=3 thru n. Define an array jj[] of prime numbers. jj[0]=2. k=k+1. All the calculated prime nos. will be stored in this array and the next no will be tested for prime by dividing by these prime numbers only. Calculate square root of I, add 1 and store in ii. xx. For all j from 2 thru ii (j to be incremented by 1) divide i by j. If for any j, for this i the remainder r=0, { skip this i & make j=1 } else print i as a prime number; k=k+1; and store in jj[k]; . increase i by 2. go to xx.

D.S. : HOW THE ALGORITHM EFFECTS TIME AND SPACE COMPLEXITY PARTHA PRATIM CHAKRAVORTY

*/To calculate & display all the prime nos upto 1,000,000, this Program takes around 18seconds in my computer. /* Prime no D.c * Author Partha Pratim Chakravorty */ /* Date : 12 Aug 2012 */ #include <stdio.h> #include <conio.h> #include <math.h> int main() { int n,i,j,m1,sqri, ind, p, pn[100000],plast; float (f); printf(" Prime no calculation D. Pl feed the upper limit n: \n"); scanf("%d",&n); sqri=ind=m1=i=0; p=1; plast=1; pn[1]=2; pn[2]=3;pn[3]=5;pn[4]=7; printf("List of PRIME numbers :: 2 ");

for(i=3; i<=n;i=i+2) { sqri=sqrt(i); ind =0; for(p=1; pn[p]<= sqri +1 ; p++) { m1=i/pn[p]; if (m1 * pn[p] == i ) ind= ind + 1;
} if (ind==0) { } getch(); return 0; }

D.S. : HOW THE ALGORITHM EFFECTS TIME AND SPACE COMPLEXITY PARTHA PRATIM CHAKRAVORTY

printf(" %d ",i); plast=plast+1;pn[plast]=i; }

/* Prime no D.c */ #include <stdio.h> #include <conio.h> #include <math.h> int main() { int n,i,j,m1,sqri, ind, p, pn[100000],plast; float (f); printf(" Prime no calculation D. Pl feed the upper limit n: \n"); scanf("%d",&n); sqri=ind=m1=i=0; p=1; plast=1; pn[1]=2; pn[2]=3;pn[3]=5;pn[4]=7; printf("List of PRIME numbers :: 2 "); for(i=3; i<=n;i=i+2) { sqri=sqrt(i); ind =0; for(p=1; pn[p]<= sqri +1 ; p++) { m1=i/pn[p]; if (m1 * pn[p] == i ) ind= ind + 1; }
if (ind==0) { } } getch(); return 0; }

D.S. : HOW THE ALGORITHM EFFECTS TIME AND SPACE COMPLEXITY PARTHA PRATIM CHAKRAVORTY

printf(" %d ",i); plast=plast+1;pn[plast]=i;

D.S. : HOW THE ALGORITHM EFFECTS TIME AND SPACE COMPLEXITY PARTHA PRATIM CHAKRAVORTY

Would you like to improve it further?

Write c programmes for the above 4 algorithms for n=1000000 and see the difference in time.

D.S. : HOW THE ALGORITHM EFFECTS TIME AND SPACE COMPLEXITY PARTHA PRATIM CHAKRAVORTY

D.S. : HOW THE ALGORITHM EFFECTS TIME AND SPACE COMPLEXITY PARTHA PRATIM CHAKRAVORTY

END of this set