Problem In Prime number Program

Hi
I programs a Prime number . code of my program is under :

////////////////////////////////////////////////////////
int main()
{

long int num,temp=0;
cin>>num;

for( int i=2 ; i<num ; i++ )
if( num%i == 0 )
temp++;
if( temp == 0 )
cout<<"\n"<<"It Is Prime number";
if( temp > 0 )
cout<<"\n"<<"It Is Not Prime number";

getch();
return 0;
}

////////////////////////////////////////////////////////

when I test this program with 2222222222 , the program said that
"It Is Prime number" and this answer is false .
what wrong is with my program ?
The greatest value that a (signed) 32 bit integer can hold is 2^31-1 (=2,147,483,647).
Try the long long/int64_t type.

Edit: besides, you can abort the search if you found at least one divisor, because then you know the number isn't prime (except for 2).
You also only need to test all numbers up to sqrt(num), because all divisors would have a counterpart (namely the result of the division) on the "other side of the square root".
Last edited on
2222222222 in signed notation is -858993460 which is less than 2. The loop is never entered.

unsigned int should work fine. Might take you a while based on your program though ;)
Last edited on
:/ correct me if I'm wrong but isn't the proof of prime numbers unsolved? AS IN: there is no equation that produces prime numbers infallibly?
Seraphimsan wrote:
:/ correct me if I'm wrong but isn't the proof of prime numbers unsolved? AS IN: there is no equation that produces prime numbers infallibly?

Proof of what?
Also, equations generally don't produce anything.
Seraphimsan wrote:
:/ correct me if I'm wrong but isn't the proof of prime numbers unsolved? AS IN: there is no equation that produces prime numbers infallibly?

You are correct. I also recall a professor of mine saying that if we manage to prove Riemann's hypothesis then we can use this to also find a formula that produces prime numbers, or something like that... I wasn't really paying attention...

http://en.wikipedia.org/wiki/Riemann_hypothesis

But, of course, nobody forbids you to test if a given number is prime by doing a bunch of divisions. It just takes more and more time as the numbers you test grow.
Topic archived. No new replies allowed.