C m和n之间的素数no
我在SPOJ中尝试了一个基本的不生成问题(链接:)。 我用的是seive算法。当我使用spoj gcc时,我得到了SIGSEGV错误。但当我使用ubuntu gcc编译时,它适用于所有测试用例 这是我的源代码。Plz帮助C m和n之间的素数no,c,C,我在SPOJ中尝试了一个基本的不生成问题(链接:)。 我用的是seive算法。当我使用spoj gcc时,我得到了SIGSEGV错误。但当我使用ubuntu gcc编译时,它适用于所有测试用例 这是我的源代码。Plz帮助 float sqroot( float x) { float a , b; a = x; // copy given value to 'a' do { b = a; // copy value of 'a' to 'b' be
float sqroot( float x)
{
float a , b;
a = x; // copy given value to 'a'
do
{
b = a; // copy value of 'a' to 'b' before 'a' is modify
a = (a + x/a) / 2; // modify 'a' value until we reach sqroot result
}
while( a!= b); // execute loop until a == b
return( a); // 'a ' or 'b' is sqroot of 'x'
}
int main()
{
int prime[4000];
int prime_index=0;
bool find_prime[100001];
int i,j;
int m,n;
int iremainder;
int T,t_index;
int PRIME_FLAG=1;
float square;
int limit;
prime_index++;
prime[prime_index]=2;
for(i=3;i<=32000;i=i+2)
{
PRIME_FLAG=1;
square = sqroot((float)i);
limit = ((int)(square))+1;
for(j=1;j<=prime_index,prime[j]<=limit;j++)
{
if(prime[j]!=0)
{
if((i%prime[j]) == 0)
{
PRIME_FLAG = 0;
break;
}
}
}
if(PRIME_FLAG)
{
prime_index++;
prime[prime_index]=i;
printf("%d\n",i);
}
}
printf("Enter the no of test cases:");
scanf("%d",&T);
if(T<=10)
{
for(t_index=1;t_index<=T;t_index++)
{
printf("Enter the values of m and n :");
scanf("%d%d",&m,&n);
if((m>=1) && (n<=1000000000) && ((n-m)<=100000))
{
if(m == 1)
m=2;
//Set all numbers from m to n as prime
for(i=m;i<=n;i++)
find_prime[i]=true;
//Find the prime numbers between m to n
square = sqroot((float)n);
limit = ((int)(square))+1;
for(i=1;i<=prime_index,prime[i]<=limit;i++)
{
if(m>=prime[i])
{
if(prime[i]!=0)
iremainder=m%prime[i];
j=prime[i]*iremainder;
}
else
{
iremainder=prime[i]-m;
if(m+iremainder == prime[i])
j=2*(m+iremainder);
else
j=m+iremainder;
}
for(;j<=n;j=j+prime[i])
find_prime[j]=false;
}
//Print all prime no's
for(i=m;i<=n;i++)
{
if(find_prime[i])
printf("%d\n",i);
}
}
}
}
return 0;
}
float sqroot(float x)
{
浮子a、b;
a=x;//将给定值复制到“a”
做
{
b=a;//在修改“a”之前将“a”的值复制到“b”
a=(a+x/a)/2;//修改“a”值,直到得到sqroot结果
}
while(a!=b);//执行循环直到a==b
return(a);/“a”或“b”是“x”的平方根
}
int main()
{
整数素数[4000];
整数素数指数=0;
bool find_prime[100001];
int i,j;
int m,n;
内部维护;
int T,T_指数;
int PRIME_标志=1;
浮法广场;
整数极限;
素数索引++;
素数[素数指数]=2;
for(i=3;i您的for循环是错误的
for(j=1;j<=prime_index,prime[j]<=limit;j++)
for(j=1;jdid)运行此代码???告诉我们哪个测试用例失败?对我来说,它工作正常。bool find_prime[100001];
以及int prime[4000];
闻起来像是堆栈溢出。我已经更新了代码,但在测试用例中,当m=100000和n=100010时,它仍然失败
for(j=1;(j<=prime_index)&&(prime[j]<=limit);j++)
for(i=1;i<=prime_index,prime[i]<=limit;i++)