0% found this document useful (0 votes)
26 views4 pages

Assignment

The document describes an algorithm to find all prime numbers smaller than or equal to a given number n using the Sieve of Eratosthenes approach. It provides the steps of the algorithm, sample C code to implement it, and outputs of the program for different values of n. The algorithm involves marking multiples of primes as composite until only primes remain.

Uploaded by

Nani sai
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views4 pages

Assignment

The document describes an algorithm to find all prime numbers smaller than or equal to a given number n using the Sieve of Eratosthenes approach. It provides the steps of the algorithm, sample C code to implement it, and outputs of the program for different values of n. The algorithm involves marking multiples of primes as composite until only primes remain.

Uploaded by

Nani sai
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Assignment-1

14}Given a number n, print all primes smaller than or equal


to n. it is also given that n is small number using sieve of
Eratosthenes approach.
Roll no’s:1601-21-747-014,1601-21-747-035,1601-21-747-056.
Algorithm:
STEP-1:start
STEP-2:Read i,j,n
STEP-3:Read p[n]
STEP-4:if(n<1),no prime numbers until n
STEP-5:for i<=n
STEP-5a:increase i by 1
STEP-6:i=2
STEP-7:for i*i<=n
STEP-7a:if p[i]!=0
STEP-7b:for j<n
STEP-7c:if p[i]*j>n , go to 7f
STEP-7d:else [p[i]*j]=0
STEP-7e:increase j by 1,go to 7c
STEP-7f:increase i by 1,go to 7a
STEP-7g:else,go to step-7
STEP-8:i=2
STEP-9:if i<=n
STEP-8a:if p[i]!=0,print p[i]
STEP-8b:increase i by 1
STEP-10:stop
Program:
//prime until 'n' using sieve of ertosthenes
#include<stdio.h>
int main()
{
int n,i,j;
printf("Enter the number\n");
scanf("%d",&n);
int p[n+1];
p[i]=i;
for(i=2;(i*i)<=n;i++)
{
if(p[i]!=0)
{
for(j=2;j<n;j++)
{
if(p[i]*j>n)
break;
else
p[p[i]*j]=0;
}
}
}
printf("\nThe prime numbers:");

for(i=2;i<=n;i++)
{
if(p[i]!=0)
printf("%d,",i);
}
return 0;
}
OUTPUTS:
Observations and Key takeaways:
This is a method devised many years ago by the mathematician Eratosthenes of Cyrene.It
involves eliminating the numbers that are know not to be prime until only the prime numbers
remain.But its effective for small range of numbers, when numbers are large it takes time to
complete the program ,show us results.

You might also like