473,327 Members | 1,952 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

Prime No. generator

Why does this program fail to run?

//FIRST n PRIMES
#include <iostream.h>
#include <math.h>

int main(void)
{
long int i,j=3,count=1;
int n,flag=0;
cout<<"How many primes?";
cin>>n;
cout<<"2, ";
while(count<n)
{
for(i=1;i<(sqrt(j)+1);++i){
if(j%i==0) {flag=1;break;}
if(j>3 && flag==0) {cout<<j<<", ";++count;}}
++j;
}
return 0;
}

Apr 21 '07 #1
4 1453
"ume$h" <fr****************@gmail.comschrieb im Newsbeitrag
news:11**********************@b75g2000hsg.googlegr oups.com...
Why does this program fail to run?

//FIRST n PRIMES
#include <iostream.h>
#include <math.h>

int main(void)
{
long int i,j=3,count=1;
int n,flag=0;
cout<<"How many primes?";
cin>>n;
cout<<"2, ";
while(count<n)
{
for(i=1;i<(sqrt(j)+1);++i){
if(j%i==0) {flag=1;break;}
if(j>3 && flag==0) {cout<<j<<", ";++count;}}
++j;
}
return 0;
}
1) How is a prime defined? Especially, what does the definition say about
dividing a prime by 1?
2) Is 3 a prime?
3) Are there any even primes except 2.
4) What happens to 'flag' once your program found a prime?
5) How often does your program compute sqrt(j) for each j?

Perhaps your answers to these questions will help you solving your problem.

HTH
Heinz
Apr 21 '07 #2
"ume$h" writes:
Why does this program fail to run?

//FIRST n PRIMES
#include <iostream.h>
#include <math.h>

int main(void)
{
long int i,j=3,count=1;
int n,flag=0;
cout<<"How many primes?";
cin>>n;
cout<<"2, ";
while(count<n)
{
for(i=1;i<(sqrt(j)+1);++i){
if(j%i==0) {flag=1;break;}
Where is the code that sets flag equal to 1?
Can you get out of the loop without setting the flag?

You seem to be using Windows so here's a tip:

Activate the task mgr. Then activate your program. If the green icon in
the task bar turns bright green, you program is in a tight loop. (As here.)
if(j>3 && flag==0) {cout<<j<<", ";++count;}}
++j;
}
return 0;
}

Apr 21 '07 #3
//This one runs(Ultimately by self attempt!)

#include <iostream.h>
#include <math.h>
int main(void)
{
long int i,j=3,count=1;
int n;
cout<<"\nHow many first primes do you want to generate? ";
cin>>n;
if(n<0) {cout<<"Wrong No.\n Enter No. Again"; cin>>n;}
if(n==1 || n>1)cout<<"2, ";
if(n>1) while(count<n)
{

int flag=0;
for(i=2;i<sqrt(j)+1;++i)

if(j%i==0)
{
flag=1;break;
}

if(flag==0)
{
++count;cout<<j<<", ";
}
++j;

}

return 0;
}

Apr 21 '07 #4
ume$h wrote:
Why does this program fail to run?
Define "fail to run". Does it not compile? Does it give erroneous output?
See FAQ 5.8 http://www.parashift.com/c++-faq-lit...t.html#faq-5.8

//FIRST n PRIMES
#include <iostream.h>
Non-standard header. #include <iostreamisntead
#include <math.h>
using namespace std; // to get cin and cout.
>
int main(void)
void in this context is superfluous and discouraged in C++.
{
long int i,j=3,count=1;
int n,flag=0;
poor practice. Better practice is one declaration per line.
Define i inside the for loop. e.g. for (int i = 1; ...)
cout<<"How many primes?";
cin>>n;
cout<<"2, ";
while(count<n)
{
for(i=1;i<(sqrt(j)+1);++i){
Hint: if you only check odd numbers for primality (see below), you can
increment i by 2 instead of 1.
if(j%i==0) {flag=1;break;}
Your problem lies here. Hint. What is the initial value of i?
What is the value of j%i, for any j, given that initial value of i?

Also, formatting -- put the body of the if on a separate line.
if(j>3 && flag==0) {cout<<j<<", ";++count;}}
Ditto here.
++j;
Hint: 2 is the only even prime. You can bump j by 2.
}
return 0;
}
Apr 21 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

36
by: Dag | last post by:
Is there a python module that includes functions for working with prime numbers? I mainly need A function that returns the Nth prime number and that returns how many prime numbers are less than N,...
9
by: Greg Brunet | last post by:
In doing some testing of different but simple algorithms for getting a list of prime numbers, I ended up getting some results that seem a bit contradictory. Given the following test program...
20
by: Tuvas | last post by:
I have made and recently posted a libary I made to do Modular Arithmetic and Prime numbers on my website at http://www.geocities.com/brp13/Python/index.html . I am currently in a crypotology...
10
by: Joel Mayes | last post by:
Hi All; I'm teaching myself C, and have written a prime number generator. It is a pretty inefficient implementation of the Sieve of Eratosthenes to calculate primes up to 1,000,000. If anyone...
5
by: maks | last post by:
Hi! I need some help in modifying this prime number generator code. How do I modify this code so that it assigns prime numbers to an array and returns it? I have tried to get it work but it...
60
by: rhle.freak | last post by:
Here is my code to generate prime numbers.It works absolutely fine when the range is *not very large*. However on initializing i with a large integer it produces erroneous results (some numbers...
4
bartonc
by: bartonc | last post by:
Description: This is a fast prime number list generator using sieve algorithm. This function return a list of prime numbers which <= argument. def primes(n): if n==2: return elif n<2:...
7
by: Caffiend | last post by:
Well, I've been picking at learning python, got tired of reading, and figured I'd try to replicate my prime number generator I wrote (with much TSDN forum help) in C++. I've hit a stumbling block......
2
by: sudankanakavel | last post by:
i need a random prime number generator which will generate prime numbers for given range for eg 22222 to 99999 operating system : windows language : java
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.