LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
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


Reply
  Search this Thread
Old 08-30-2004, 02:00 PM   #1
muddlnx
LQ Newbie
 
Registered: Aug 2004
Location: Tennessee
Distribution: Slackware, Ubuntu
Posts: 27

Rep: Reputation: 15
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 <iostream>

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.
 
Old 08-30-2004, 02:06 PM   #2
aluser
Member
 
Registered: Mar 2004
Location: Massachusetts
Distribution: Debian
Posts: 557

Rep: Reputation: 43
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.
 
Old 08-30-2004, 06:24 PM   #3
Brane Ded
Member
 
Registered: Nov 2003
Location: over there
Distribution: Debian Testing
Posts: 191

Rep: Reputation: 30
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.
 
Old 08-30-2004, 06:27 PM   #4
aluser
Member
 
Registered: Mar 2004
Location: Massachusetts
Distribution: Debian
Posts: 557

Rep: Reputation: 43
hah, I stopped reading when I found the first one
 
Old 08-31-2004, 12:23 AM   #5
muddlnx
LQ Newbie
 
Registered: Aug 2004
Location: Tennessee
Distribution: Slackware, Ubuntu
Posts: 27

Original Poster
Rep: Reputation: 15
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?
 
Old 08-31-2004, 12:33 AM   #6
cppkid
Member
 
Registered: Jul 2004
Location: Pakistan
Distribution: Ubuntu
Posts: 185

Rep: Reputation: 30
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 ...............
 
Old 08-31-2004, 10:14 PM   #7
rustynailz
Member
 
Registered: Jun 2004
Distribution: MDK 9.2/10.0, VectorLinux 4.0
Posts: 50

Rep: Reputation: 15
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).
 
  


Reply


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



Similar Threads
Thread Thread Starter Forum Replies Last Post
RSA prime numbers? tarballedtux Linux - Security 19 06-28-2008 06:12 PM
C Prime Generator Chrax Programming 9 12-23-2004 01:02 AM
Adding numbers, break on non-numbers... Cruger Programming 1 03-22-2004 09:18 AM
Algo for Relatively prime No. LinuxTiro Programming 5 11-17-2003 09:02 PM
magic numbers problem muneebs123 Programming 2 09-05-2003 01:33 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:15 AM.

Main Menu
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.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration