hello ebrahim. u have a bit wrong in your code.
please compare this two code.
code 1:
for (int i = 2; i < MAXP; ++i)
if (!comp[MAXP])
{
primes[nprimes++] = i;
for (int j = i; j < MAXP; j += i)
comp[j] = true;
}
_____________________________________________________________
code 2:
for (int i = 2; i < MAXP; ++i)
if (!comp)
{
primes[nprimes++] = i;
for (int j = i; j < MAXP; j += i)
comp[j] = true;
}
yes, your wrong is comp[MAXP]. you must replace i with comp.
GOOD LUCK.
MASOUD
10140 - Prime Distance
Moderator: Board moderators
-
- New poster
- Posts: 3
- Joined: Thu Aug 25, 2011 5:26 pm
Re: 10140 - Prime Distance
run time error if anybody give me solution it would be better for me
plz help me to solve the problem...................
here is my code..................
plz help me to solve the problem...................
here is my code..................
Last edited by Dhiman Das on Fri Aug 26, 2011 12:28 am, edited 2 times in total.
-
- New poster
- Posts: 3
- Joined: Thu Aug 25, 2011 5:26 pm
Re: 10140 - Prime Distance
run time error anybody give me a solution
include<stdio.h>
#include<math.h>
#define max 2147483647
unsigned long int prime[max];
void sieve()
{
prime[1]=0;
unsigned long int n,s;
for(n=2;n<=max;n++)
prime[n]=1;
for(n=2;n<=sqrt(max);n++)
if(prime[n])
for(s=2;s<=max/n;s++)
prime[s*n]=0;
}
int main()
{
sieve();
unsigned long int up,low,min,ma,i,pr[100000000],c,sub,x,y;
while(scanf("%lu %lu",&low,&up)==2)
{ c=0;
for(i=low;i<=up;i++)
if(prime)
pr[c++]=i;
min=10000000;
ma=0;
x=0;
y=0;
sub=0;
if(c>1)
{
for(i=0;i<c-1;i++){
sub=pr[i+1]-pr;
if(sub>ma){
ma=sub;
x=i;
}
if(sub<min){
min=sub;
y=i;
}
}
}
if(c>1)
printf("%lu,%lu are closest, %lu,%lu are most distant.\n",pr[y],pr[y+1],pr[x],pr[x+1]);
else
printf("There are no adjacent primes.\n");
}
return 0;
}
include<stdio.h>
#include<math.h>
#define max 2147483647
unsigned long int prime[max];
void sieve()
{
prime[1]=0;
unsigned long int n,s;
for(n=2;n<=max;n++)
prime[n]=1;
for(n=2;n<=sqrt(max);n++)
if(prime[n])
for(s=2;s<=max/n;s++)
prime[s*n]=0;
}
int main()
{
sieve();
unsigned long int up,low,min,ma,i,pr[100000000],c,sub,x,y;
while(scanf("%lu %lu",&low,&up)==2)
{ c=0;
for(i=low;i<=up;i++)
if(prime)
pr[c++]=i;
min=10000000;
ma=0;
x=0;
y=0;
sub=0;
if(c>1)
{
for(i=0;i<c-1;i++){
sub=pr[i+1]-pr;
if(sub>ma){
ma=sub;
x=i;
}
if(sub<min){
min=sub;
y=i;
}
}
}
if(c>1)
printf("%lu,%lu are closest, %lu,%lu are most distant.\n",pr[y],pr[y+1],pr[x],pr[x+1]);
else
printf("There are no adjacent primes.\n");
}
return 0;
}