Visit Jeremy's Blog.
 Home Forums HCL Reviews Tutorials Articles Register Search Today's Posts Mark Forums Read
 LinuxQuestions.org c++ prime numbers loop problem
 User Name Remember Me? Password
 Programming This forum is for all programming questions. The question does not have to be directly related to Linux and any language is fair game.

Notices

 08-30-2004, 02:00 PM #1 muddlnx LQ Newbie   Registered: Aug 2004 Location: Tennessee Distribution: Slackware, Ubuntu Posts: 27 Rep: c++ prime numbers loop problem hey, i have been reading these forums for a while but just signed for an account. any how, here is my problem, i'm kind of new to c++, i'd i thought i'd dabble a little to get use to it. i'm writing a program to specify prime numbers in a given range, but i don't believe it is runnig my functions, or that it is not going through the loops, here is my code #include using namespace std; unsigned long a , b; int prime(unsigned long number); int lookforprime(unsigned long lowerNumber, unsigned long upperNumber); int main(){ cout << "This program will determine if there are any prime numbers" << endl; cout << "in a range that you specify." << endl; cout << "Enter the lower limit: "; cin >> a; cout << "Enter the upper limit: "; cin >> b; lookforprime(a , b); cout << "Thanks, Come Again." << endl; return 0;} int lookforprime(unsigned long lowerNumber , unsigned long upperNumber){ for( unsigned long i = lowerNumber ; i == upperNumber ; i++){ prime(i); cout << "program is running" << endl;} return 0;} int prime(unsigned long number){ for(unsigned long divisor = --number ; divisor == 1 ; divisor--){ if((number % divisor) == 0){ cout << number << " is not prime"; return 0;} cout << number << " is prime" << endl; return 0; }} any ideas? thanks for looking.
 08-30-2004, 02:06 PM #2 aluser Member   Registered: Mar 2004 Location: Massachusetts Distribution: Debian Posts: 557 Rep: the for loop in lookforprime() should be Code: ```for (unsigned long i = lowerNumber; i <= upperNumber; i++) { ....... }``` You just got the wrong boolean operator. You might want to check somewhere that lowerNumber is actually lower than upperNumber, too.
 08-30-2004, 06:24 PM #3 Brane Ded Member Contributing Member   Registered: Nov 2003 Location: over there Distribution: Debian Testing Posts: 191 Rep: Code: ```for(unsigned long divisor = --number ; divisor == 1 ; divisor--) { ... }``` divisor == 1 should probably be devisor >= 1. It still won't find prime numbers that way, but it will do the loop now. Also, you can reuse the i variable that you used in the other loop. It goes out of scope once the loop's over. Last edited by Brane Ded; 08-30-2004 at 06:26 PM.
 08-30-2004, 06:27 PM #4 aluser Member   Registered: Mar 2004 Location: Massachusetts Distribution: Debian Posts: 557 Rep: hah, I stopped reading when I found the first one
 08-31-2004, 12:23 AM #5 muddlnx LQ Newbie   Registered: Aug 2004 Location: Tennessee Distribution: Slackware, Ubuntu Posts: 27 Original Poster Rep: thanks to both of you the loop does work, i've been out of programming so long, i just need to get use to it again. Brane Ded can you tell me why it won't work without telling me how to make it work?
08-31-2004, 12:33 AM   #6
cppkid
Member

Registered: Jul 2004
Location: Pakistan
Distribution: Ubuntu
Posts: 185

Rep:
Quote:
 divisor == 1 should probably be devisor >= 1.
It will not work because the devisor >= 1, the loop will execute for
devisor > 1 and also for devisor == 1.
So as each number is divisible by 1 and (any_number % 1 ) is always 0.
So ...............

 08-31-2004, 10:14 PM #7 rustynailz Member   Registered: Jun 2004 Distribution: MDK 9.2/10.0, VectorLinux 4.0 Posts: 50 Rep: Thought of using Fermat's Little Theorem instead of a brute force search to test for primality? Also, you don't need to start at (number-1), but at sqrt(number).

 Thread Tools Search this Thread Search this Thread: Advanced Search

 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 Off Forum Rules

 Similar Threads Thread Thread Starter Forum Replies Last Post tarballedtux Linux - Security 19 06-28-2008 06:12 PM Chrax Programming 9 12-23-2004 01:02 AM Cruger Programming 1 03-22-2004 09:18 AM LinuxTiro Programming 5 11-17-2003 09:02 PM muneebs123 Programming 2 09-05-2003 01:33 PM

LinuxQuestions.org

All times are GMT -5. The time now is 06:04 PM.

 Contact Us - Advertising Info - Rules - LQ Merchandise - Donations - Contributing Member - LQ Sitemap -
 Advertisement
 My LQ
 Write for LQ LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
 Syndicate Latest Threads   LQ News Twitter: @linuxquestions Facebook: linuxquestions Google+: linuxquestions