0% found this document useful (0 votes)
114 views

DATA Structures 11 How The Algo Effects Time and Space Complexity

The document describes four algorithms for calculating prime numbers from 1 to n and analyzes how they affect time and space complexity. Algorithm A checks all numbers from 2 to n for primality using nested loops. Algorithm B improves on A by only checking odd numbers. Algorithm C further optimizes B by only checking up to the square root of the number. Algorithm D stores previously found prime numbers in an array and only checks for divisibility against those. The algorithms get progressively faster, with A taking 65 seconds for n=100,000, B taking 35 seconds, C taking 3-5 seconds, and D taking 18 seconds for n=1,000,000.

Uploaded by

Kushal Biswas
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
114 views

DATA Structures 11 How The Algo Effects Time and Space Complexity

The document describes four algorithms for calculating prime numbers from 1 to n and analyzes how they affect time and space complexity. Algorithm A checks all numbers from 2 to n for primality using nested loops. Algorithm B improves on A by only checking odd numbers. Algorithm C further optimizes B by only checking up to the square root of the number. Algorithm D stores previously found prime numbers in an array and only checks for divisibility against those. The algorithms get progressively faster, with A taking 65 seconds for n=100,000, B taking 35 seconds, C taking 3-5 seconds, and D taking 18 seconds for n=1,000,000.

Uploaded by

Kushal Biswas
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 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

You might also like