0

i m trying 2 find all primes between 2 nos using sieve.
but somehow only 2 is being set as prime, pls help me in finding the problem

#include<stdio.h>//1==t for prime
#include<stdlib.h>
#define MAX_LIMIT 100
char flags[MAX_LIMIT+100];  
void set_seive(void)
{
	
/*setting of flags for prime nos. by sieve of erasthromas upto MAX_LIMIT*/

unsigned long long i,p;
flags[1]='f';  /*1 is not prime*/
flags[2]='t';
for(i=3;i<=MAX_LIMIT;i++)
{
	
	if(i%2==0)
	flags[i]='f';  /*not prime bcoz even*/
	else
	flags[i]=='t';
}

for(p=3;p<=MAX_LIMIT ;p++) //initial 
{
	if(flags[p]=='f')
		continue;
	for(i=2;i*p<=MAX_LIMIT;i++)
		flags[i*p]='f';     /*2p,3p,4p  are not prime*/

}

}

int main()
{
	set_seive();
	unsigned long long m,n,i;
	int t;
	scanf("%d",&t);
	while(t--)
	{
		scanf("%llu %llu",&m,&n);
		for(i=m;i<=n;i++)
		{
			if(flags[i]=='t')
			printf("%llu\n",i);
		}
		putchar('\n');
	}
	system("pause");
	return 0;
}

here t is the no. of test cases and m,n are the nos. between which primes r 2 b found

Edited by karan173: mistake

2
Contributors
1
Reply
4
Views
5 Years
Discussion Span
Last Post by WaltP
0

Try this:

Initialize all values as 'prime'.

Outer loop [B]val1[/B] from 2 to [I]whatever[/I] 
    Inner loop [B]val2[/B] from [B]val1[/B]*2 by steps of [B]val1[/B]
                                   // skips the number itself ...
        sets value to 'not prime'  // ... and sets each multiple thereafter
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.