Page 2 of 2

Re: 10140 - Prime Distance

Posted: Wed Feb 10, 2010 9:58 am
by masoud1459
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

Re: 10140 - Prime Distance

Posted: Fri Aug 26, 2011 12:16 am
by Dhiman Das
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..................

Re: 10140 - Prime Distance

Posted: Fri Aug 26, 2011 12:26 am
by Dhiman Das
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;
}