Wrox Programmer Forums Prime Number Counting. Help!!
 | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
 C++ Programming General discussions for the C++ language. For questions specific to Microsoft's Visual C++ variant, see the Visual C++ forum instead.
 Welcome to the p2p.wrox.com Forums. You are currently viewing the C++ Programming section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersâ€™ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
August 10th, 2004, 06:39 AM
 Registered User Join Date: Aug 2004 Location: , , . Posts: 3 Thanks: 0 Thanked 0 Times in 0 Posts
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);

}

August 10th, 2004, 10:10 AM
 Authorized User Join Date: Feb 2004 Location: , , . Posts: 76 Thanks: 0 Thanked 0 Times in 0 Posts

Two fundamental problems:

1. You declare a[5] and j[5], then use values of j==1,2,3,4,5. Should be 0,1,2,3,4.

2. You have declared nprimes[] to be of type double, but try to print its value with a %d format specifier. Use %lf for type double.

In fact, I feel that a[] and nprimes[] should be declared and used as type int, since they are used to indicate quantities that have integer values.

In fact, I feel that everything should be type int, since "prime number" only makes since with integers. Why did you use doubles?

I haven't checked your logic to see if the answers you get are correct. I had to leave something for you to do.

Here are the correct answers for the first four:

The number of primes less than 100 is 25
The number of primes less than 1000 is 168
The number of primes less than 10000 is 1229
The number of primes less than 100000 is 9192

I hope this helps.

Regards,

Dave
August 11th, 2004, 02:00 AM
 Friend of Wrox Join Date: Jul 2004 Location: Tehran, , Iran. Posts: 623 Thanks: 0 Thanked 1 Time in 1 Post

Dave thank u;
I think he has to use double because sqrt accept only double values(in my compiler using math.h)but it doesn't need using Floor...
in addition to points Dave mentioned I think if you change your testprime like below it could be better and faster..
Code:
```int testprime(int c)
{
int a;//keeps sqrt of your number
a=sqrt((double)c);
int counter=2;
while(counter<=a)
{
if(c%counter==0) return 0;
counter++;
}
return 1;//if your number is prime it returns 1 otherwise 0
}```
--------------------------------------------
Mehdi.:)
August 27th, 2004, 12:00 PM
 Registered User Join Date: Aug 2004 Location: , , Slovakia. Posts: 2 Thanks: 0 Thanked 0 Times in 0 Posts

For problem of finding primes, the best solution is Eratostenes Sieve.
Here is my a code for your problem:

int count_of_primes(int n){ //We are finding primes between 1..n
int i,j;
int count=0;
bool prime[1000001];

for(i=2;i<=n;i++) prime[i]=true;
for(i=2;i<=sqrt(n);i++) if (prime[i]){
for(j=i*2;j<=n;j+=i) prime[j]=false;
}
for(i=2;i<=n;i++) if (prime[i]) count++;
return(count);
}

http://mathworld.wolfram.com/SieveofEratosthenes.html

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is Off HTML code is OffTrackbacks are Off Pingbacks are On Refbacks are Off Forum Rules

 Similar Threads Thread Thread Starter Forum Replies Last Post Validation For Phone Number and Mobile Number dhruthi.ram99 Javascript How-To 12 October 30th, 2011 07:24 AM Counting Number of Rows Between Data Range eusanpe Excel VBA 6 September 21st, 2006 07:17 AM Counting number of spaces in a line Suomi Access VBA 3 September 9th, 2005 02:34 PM prime numbers code error.. amahja56 C++ Programming 1 January 19th, 2005 01:33 PM

All times are GMT -4. The time now is 05:53 PM.