In fact, before any input is read, you should think about a thing.
If case 1 is the interval 39 to 400 and the case 2 is the 39 to 400 again, you will run two times for the answer.
So make sure you can tackle the problem.
i can't get yours points. yours progremm and mine are both ok, i can run them on my computer.
int ifprime(int n) /*justify whether it is a prime*/
{int j;
for(j=2;j<n;j++)
{if (n%j!=0) continue;
else return (0);}
if(n==j)
return (1);
}
int creat_num(int n)
{int m;
m=n*n + n + 41;
return m; /*creat a new number with Euler formule by the given number */
}
main()
{int a,b,i;
float pre,sum;
int re=0;
clrscr();
scanf("%d %d",&a,&b);
for(i=a;i<=b;i++)
re=re+ifprime(creat_num(i)); /*creat a number, justify whether it is a prime and then if it is then retuen a "1" ,or a 0" */
i haven't got any algorithm for genarating prime number fast . My way of generating prime is obviously very slow. i have got two code of generating prime from the board but cant use it properly as i am a new person here in acm . so it will be a great help if u can tell me how to use that two algorithms.Bye
Riyad
#include<stdlib.h>
#include<malloc.h>
#define isprime(f,x) (*(f+(x)/16)&(1<<(((x)%16L)/2)))
#define setprime(f,x) *(f+(x)/16)|=1<<(((x)%16L)/2)
void main()
{
unsigned char *field=NULL,*zzz;
unsigned long teste=1,max,mom,count,alloc;
int d;
max=20010000L;
while(field==NULL)
zzz=field=(unsigned char*) malloc(alloc=(((max-=10000L)>>4)+1L));
for (count=0;count<alloc;count++)
*zzz++ = 0x00;
while((teste+=2)<max)
if(!isprime(field,teste))
for(mom=3L*teste;mom<max;mom+=teste<<1)
setprime(field,mom);
if(isprime(field,3)==1)
/*now you can call the module isprime to check any positive integer
either prime or not;
syntax:
isprime(field,integer);
if the module return 0 the integer will be prime
else not prime.
you can check upto 20000000 within a while
*/
free (field);
}
/* ***********************************************/
MY QUESTION----->
/*what should i use in place of the parameter "field" in the function
isPrime(field , integer). that means what should i do if i want to check the primality of the number 100010041.*/
/*********************************************/
/**********************SIEVE********************************/
#define MAXSIEVE 100000000 // All prime numbers up to this
#define MAXSIEVEHALF (MAXSIEVE/2)
#define MAXSQRT 5000 // sqrt(MAXSIEVE)/2
char a[MAXSIEVE/16+2];
#define isprime(n) (a[(n)>>4]&(1<<(((n)>>1)&7))) // Works when n is odd
int i,j;
memset(a,255,sizeof(a));
a[0]=0xFE;
for(i=1;i<MAXSQRT;i++)
if (a[i>>3]&(1<<(i&7)))
for(j=i+i+i+1;j<MAXSIEVEHALF;j+=i+i+1)
a[j>>3]&=~(1<<(j&7));
Sorry for my being too stupid to paste a code that i even dont understand
HOLD ME NOW ,, I AM 6 FEET FROM THE EDGE AND I AM THINKIN.. MAY BE SIX FEET IS SO FAR DOWN
The way you generate the primes is too costly wiht respect to time.
One method is to use existing primes to find new primes.
here is the algorithm.
[cpp]
primes[MAX];
genprime()
{
prime[0]=2; prime[1]=3;prime[2]=5;prime[3]=7;
count=4;
long int num,i;
for(num=11;num<MAX;num=num+2)
{
for(j=0;j<count;j++)// can be made faster if j<sqrt(prime[count-1] is used
if(num%prime[j]==0) break;
if(j==count)
prime[count++]=num;
}
}
[/cpp]