Need help with prime numbers program

B

brian.digipimp

Write a program that prompts the user to input a positive integer. It
should then output a message indicating whether the number is a prime
number. (Note: An even number is prime if it is 2. An odd integer is
prime if it is not divisible by an odd integer less than or equal to
the square root of the number.) I was given this assignment in my c++
class and I'm having trouble with it. I've looked at the professors lab
solutions but I just dont understand it. This is the code I have.

#include <iostream>
#include <cmath>

using namespace std;
int isprime(int);

int main()
{
int inputnumber;

cout << "Input a positive integer:";
cin >> inputnumber;

if (isprime(inputnumber))
cout << inputnumber << " is prime";
else
cout << inputnumber << " is not prime";


int isprime(int inval);


int retval = 1;
int counter = 2;
int inval;

while ((counter < sqrt(retval)) && retval != 0)
{
if (inval % counter == 0)
retval = 0;
counter++;

return retval;
}

return 0;
}

Im getting an ambigous call to overload on my square root function.
Other than that the program is just not working, can someone please
help me out here?
 
M

Mark P

Write a program that prompts the user to input a positive integer. It
should then output a message indicating whether the number is a prime
number. (Note: An even number is prime if it is 2. An odd integer is
prime if it is not divisible by an odd integer less than or equal to
the square root of the number.) I was given this assignment in my c++
class and I'm having trouble with it. I've looked at the professors lab
solutions but I just dont understand it. This is the code I have.

#include <iostream>
#include <cmath>

using namespace std;
int isprime(int);

int main()
{
int inputnumber;

cout << "Input a positive integer:";
cin >> inputnumber;

if (isprime(inputnumber))
cout << inputnumber << " is prime";
else
cout << inputnumber << " is not prime";


int isprime(int inval);

1. This is a function declaration since it ends with a semicolon-- you
need a function definition (it was already declared above anyway).
2. You can't define a function within main. You need to close main and
define this function separately (either before or after main).
3. Why return an integer from what is clearly a true/false function?
That is what bool is for.
int retval = 1;
int counter = 2;

I'd give counter a more suggestive name, like divisor. Also since the
candidate divisors are 2,3,5,7,9,..., it would be more efficient to test
for 2 separateley, and then do the odds in the loop so that you can
increment divisor by +2 in each iteration.
int inval;

You don't need to declare a variable which is already passed in to the
function as a named parameter.
while ((counter < sqrt(retval)) && retval != 0)

Look at the first condition-- do you see that it will *never* be true?
Think carefully about what you're comparing (yet more reason to use
clear and descriptive variable names). Also, you need <= not <, but see
my note at the end of this post.
{
if (inval % counter == 0)

As soon as this condition is satisfied you know the number is not prime.
It makes sense to then immediately return false. Doing so will also
allow you to simplify your while condition (do you see why?)
retval = 0;
counter++;

return retval;

You're missing brackets and/or using funky spacing so I can't quite tell
what you're trying to do here. Try taking my advice in the previous
paragraph and rewriting the rest of this with properly matched brackets.
}

return 0;
}

Im getting an ambigous call to overload on my square root function.

There are several different sqrt functions defined in cmath. Try
sqrt(static_cast<double> (...)).

Also see:

http://www.dinkumware.com/manuals/reader.aspx?lib=cpl&h=math.html#sqrt

to see the various sqrt functions available.

Incidentally, rather than checking a <= sqrt(b) it's probably faster and
safer (by avoiding floating point inaccuracies) to check a*a <= b.
Other than that the program is just not working, can someone please
help me out here?

Give it another try and show us what you come up with.

Mark
 
J

John Harrison

Write a program that prompts the user to input a positive integer. It
should then output a message indicating whether the number is a prime
number. (Note: An even number is prime if it is 2. An odd integer is
prime if it is not divisible by an odd integer less than or equal to
the square root of the number.) I was given this assignment in my c++
class and I'm having trouble with it. I've looked at the professors lab
solutions but I just dont understand it. This is the code I have.

#include <iostream>
#include <cmath>

using namespace std;
int isprime(int);

int main()
{
int inputnumber;

cout << "Input a positive integer:";
cin >> inputnumber;

if (isprime(inputnumber))
cout << inputnumber << " is prime";
else
cout << inputnumber << " is not prime";

Missing }
int isprime(int inval);

Extra illegal ;
int retval = 1;
int counter = 2;
int inval;

while ((counter < sqrt(retval)) && retval != 0)
{
if (inval % counter == 0)
retval = 0;
counter++;

return retval;
}

return 0;
}

Im getting an ambigous call to overload on my square root function.
Other than that the program is just not working, can someone please
help me out here?

Well the loop is confused

1) It should be sqrt(inval) not sqrt(retval)

2) It should be counter <= sqrt(inval) not counter < sqrt(inval)

3) The ambiguous error message is probably because sqrt comes in two
version, one for float and one for double, and you provided neither.

4) Anyway you don't need sqrt

counter <= sqrt(inval)

is the same as

counter*counter <= inval

At least that's good enough for a newbie program.

5) You have put return retval in your loop for some reason best known to
yourself. If you look at the loop you *always* return retval from inside
the loop.

6) Anyway you don't need retval

while (counter*counter <= inval)
{
if (inval % counter == 0)
return 1;
counter++;
}
return 0;

7) isprime is a boolean function so it should return a boolean not an
integer

bool isprime(int inval)
{
...
return true;
...
return false;
}

This should be a good lesson, in programming the details matter, its not
good enough to be 80% right, it has to be 100%.

john
 
B

brian.digipimp

Ok, I really appreciate your help on this guys. I changed a few things
and added in some comments. I'm still having a few problems compilling
this program though. It seems im having an error at my if statement...

if primenum(inputnumber)
cout << inputnumber << " is prime";
else
cout << inputnumber << " is not prime";


syntax error: identifier 'primenum'
illegal else without matching if
 
M

Mark P

Ok, I really appreciate your help on this guys. I changed a few things
and added in some comments. I'm still having a few problems compilling
this program though. It seems im having an error at my if statement...

if primenum(inputnumber)
cout << inputnumber << " is prime";
else
cout << inputnumber << " is not prime";


syntax error: identifier 'primenum'
illegal else without matching if

In general it's always a good idea to post complete code because the
problem may not be what you think it is (or maybe there's more than one
problem). That said, your problem here is the "if" syntax which should be:

if (primenum(inputnumber))
....

The condition needs to be enclosed in parentheses.
 
B

brian.digipimp

Mark, thank you so much for your quick reply. If you look up at the top
I had it correct, but I took your advice and re-wrote most of the
program and I didn't notice I had changed that. Thank you for the help.
 
C

cdiggins

I think you should break the entire problem up into easy to chew
chunks. Don't be afraid to write lots of functions. It makes writing
the program trivial. All of this retval, intval, counter stuff is
confusing, even for me. Here is how I would approach the problem:

This tells me I need a function such as:

int PromptUser() {
cin >> n;
if (n <= 0) {
exit(1);
}
return n;
}
number.

This indicates I need the following function:

void OutputMessage(bool bIsPrime) {
if (bIsPrime) {
cout << "the number is prime" << endl;
}
else {
cout << "the number is not prime" << endl;
}
}

bool IsNumberEven(int n) {
return n % 2 == 0;
}

bool IsEvenNumberPrime(int n) {
assert(IsNumberEven(n)); // make sure it really is an even number
if (n == 2) {
return true;
}
else {
return false;
}
}
prime if it is not divisible by an odd integer less than or equal to
the square root of the number.)

bool IsNumberOdd(int n) {
return !IsNumberEven(n);
}

bool IsDivisibleBy(int Dividend, int Divisor) {
return Dividend % Divisor == 0;
}

bool IsOddNumberPrime(int n) {
assert(IsNumberOdd(n)); // make sure it really is an odd number
double dSquareRoot = sqrt(n);
// check divisibility against all odd numbers up to the square root.
for (int i=1; i <= dSquareRoot; ++i) {
if (IsNumberOdd(i)) {
if (IsDivisibleBy(n, i)) {
return false;
}
}
}
return true;
}

I'll leave it to you to construct the rest of program.

I hope this helps.

Christopher Diggins
http://www.cdiggins.com
http://www.cpp-cookbook.com
 
J

John Harrison

Mark, thank you so much for your quick reply. If you look up at the top
I had it correct, but I took your advice and re-wrote most of the
program and I didn't notice I had changed that. Thank you for the help.

'I rewrote most of the program'

I'm impressed, you'll go far. Hanging around here I'm continually
frustrated by how many newbies are reluctant to do that. Of course
should you ever get a job programming your bosses will tell you not to
rewrite poor code but you'll do it anyway.

john
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top