Prime Number Counting. Help!!
Hi, i'm a c beginner and have to write a programme to count the number of prime numbers less than 100,1000,10000,100000,1000000 respectively. I've written the following programme but when i run it i keep getting seros! please help!
floor() is a function which returns the integer value of the number.
#include<catam.h> //software library of my university
/*Function test prime tod etermine if number is prime or not. Returns Value called result =1for prime, 0 for non prime*/
double testprime(double h)
{
double result=1 ,a,i=2;// default result =0, i starts at 2 to avoid dividing my 1 and always gettin an integer
double x,m;
m=sqrt(h);
do
{
x=h/(i);
a=floor(x);
if(x==a) //if x is integer, result=o ie not prime
{ result=0; }
i++;
}
while(i<=m); //we only divide my i when i less than root(h)
return result;
}
/*function countprime counts the number of primes in the value given returns a count*/
double countprime(double a)
{
double nprime,n, i=2,result=nprime;
nprime=0;
do
{
result=testprime(i);
if(result==1) //if number is prime, add one to total of primes
{
nprime=nprime+1;
}
i=i+1;
}
while (i<=a); //only go as far as a
return nprime;
}
int main(void)
{int j=1;
double a[5],nprimes[5];
a[1]=100;
a[2]=1000;
a[3]=10000;
a[4]=100000;
a[5]=1000000;
do
{
nprimes[j]=countprime(a[j]);
printf("%4d\n",nprimes[j]); //print number of primes for value of a
j++;
}
while (j<=5);
}
|