views:

443

answers:

6

Hello. I am trying to make a program that determines if the number is prime or composite. I have gotten thus far. Could you give me any ideas so that it will work? All primes will , however, because composites have values that are both r>0 and r==0, they will always be classified as prime. How can I fix this?

int main()
{
    int pNumber, limit, x, r;               
    limit = 2;
    x = 2;

    cout << "Please enter any positive integer: " ;
    cin >> pNumber;

    if (pNumber < 0)
    {
        cout << "Invalid. Negative Number. " << endl;
        return 0;
    }
    else if (pNumber == 0)
    {   
        cout << "Invalid. Zero has an infinite number of divisors, and therefore neither composite nor prime." << endl;
        return 0;
    }
    else if (pNumber == 1)
    {
        cout << "Valid. However, one is neither prime nor composite" << endl;
        return 0;
    }
    else
    {
        while (limit < pNumber)
        {
            r = pNumber % x;
            x++;
            limit++;

            if (r > 0)
                cout << "Your number is prime" << endl;
            else 
            {
                cout << "Your number is composite" << endl;
                return 0;
            }
        }
    }

    return 0;
}
+3  A: 

Check out http://en.wikipedia.org/wiki/Prime_number and http://en.wikipedia.org/wiki/Primality_test

The simplest primality test is as follows: Given an input number n, check whether any integer m from 2 to n − 1 divides n. If n is divisible by any m then n is composite, otherwise it is prime.

Pierre-Antoine LaFayette
You only need to check up to sqrt(n).
Maynza
Simplest. Agreed. But complexity can be considerably reduced using Deterministic Miller-Rabin Primality check.
N 1.1
@nvl: the number being tested is an `int`. Miller-Rabin is massive overkill, and I'm not sure it's even faster on a 32bit int. An algorithm which relies on the Riemann Hypothesis for its correctness is barely worth mentioning to someone who's struggling to write their first ever primality test correctly ;-)
Steve Jessop
@steve jessop: hmm may be :). Atleast, he should know that prime numbers are of the form 6k+1, 6k-1. (~ 2,3)
N 1.1
A: 

For one thing, you'll want to break out of your loop when you find some x where pNumber % x == 0. All you need to do is find one factor of pNumber greater than 1 and less than pNumber to prove it's not prime -- no point in searching further. If you get all the way to x = pNumber without finding one, then you know pNumber is prime. Actually, even if you get to the square root of pNumber without finding one, it's prime, since if it has a factor greater than that, it should have a factor less than that. Make sense?

Tim Goodman
The key point is you have the right idea that `r==0` shows you it's not prime . . . but if you don't *stop* when you find some `r==0`, and instead keep going and overwrite it with some `r!=0`, then you're throwing out the proof that it's not prime and mistakenly marking it as prime.
Tim Goodman
yea, I'm having trouble on the stopping part. How am I suppose to break on that.
Sagistic
You almost answered your own question. Look up the keyword `break`.
MatrixFrog
lol, I know your supposed to use break, however, I'm not sure how. I've edited to script, but now its declaring all the primes b4 it tells u that its a composite. lol
Sagistic
The line where you tell it to cout<<"Prime" is used anytime r > 0, which doesn't necessarily mean it was prime, only that the number you tried to divide it by didn't go in evenly.
Maynza
yes, so when i input a number like 13, it'll display "your number is prime" 12 times.. haha.. let me try to fix that
Sagistic
A: 

I don't know what you have been taught thus far, but my discrete mathematics teacher was a fan of the Miller-Rabin test. It is a pretty accurate test that is very easy to code, within a few base tests you have a very negligible chance that you have a Carmichael Number. If you haven't gotten that far in your studies I would just stick to some basic division rules for numbers.

Maynza
+1  A: 
#include <iostream>
#include <math.h>
// Checks primality of a given integer
bool IsPrime(int n)
{
    if (n == 2) return true;
    bool result = true;
    int i = 2;
    double sq = ceil(sqrt(double(n)));
    for (; i <= sq; ++i)
    {
        if (n % i == 0)
            result = false;
    }
    return result;
}
int main()
{
    std::cout << "NUMBER" << "\t" << "PRIME" << std::endl;
    for (unsigned int i = 2; i <= 20; ++i)
        std::cout << i << "\t" << (IsPrime(i)?"YES":"NO") << std::endl; 
    std::cin.get();
    return 0;
}
Rajendra Kumar Uppal
+1  A: 
bool check_prime(unsigned val) { 

    if (val == 2)
       return true;

    // otherwise, if it's even, it's not prime.
    if ((val & 1) == 0)
        return false;

    // it's not even -- only check for odd divisors.
    for (int i=3; i*i<=val; i+=2)
       if (val % i == 0)
           return false;

    return true;
}
Jerry Coffin
This function thinks 4, 8, and 10 are prime numbers.
M Perry
@M Perry: oops, that's what I get for typing code in without testing it. I missed a set of parens (now fixed). Thanks for pointing it out.
Jerry Coffin
It also thinks 1 is prime, but anyone passing 1 into a primality check probably deserves what they get.
Steve Jessop
@Steve: Returning a bool restricts us to a yes/no answer, and for 1 there isn't really one -- for some purposes, it's convenient to treat it as prime, others as composite, and still others as neither one. "Neither" is most common, but hardly the only answer.
Jerry Coffin
That's what I mean by "deserves what they get", it's a bit like asking whether the natural numbers start from 0 or 1. I have a maths degree, though, so as it happens I've been trained that 1 is not prime. This makes the definition of a prime longer, and the statement of the fundamental theorem of arithmetic shorter. I've never seen 1 stated to be composite, that I remember. In ring theory there's the concept of "units", which are explicitly excluded from being either prime or irreducible. The concept is nearly degenerate for the integers, though, since there are only two units (1 and -1).
Steve Jessop
A: 

Simplest method is for a given number n , if it is perfectly divisible with any number between 2 to sqrt(n), its a composite, or else its prime

Kazoom