• Welcome to Overclockers Forums! Join us to reply in threads, receive reduced ads, and to customize your site experience!

C++ problem

Overclockers is supported by our readers. When you click a link to make a purchase, we may earn a commission. Learn More.

Romulox

Member
Joined
Oct 4, 2005
Location
NJ
I wrote a C++ program to find if a number is prime. The program works but the issue is that it can't use large numbers with about 11 digits such as 9999999999.
Every number lower works. Can someone please point out why it would do this.
Code:
//Program to tell if a number is a prime number

#include <iostream>
using namespace std;

void get_input(int& num);
//Get input
bool divide(int num);
//true or false
void out_put(int num);
//output data
int main()
{
	cout<<"Type 0 to stop"<<endl;
	int num;

	while (num!=0)
	{
	get_input(num);
    
	divide(num);
	
	out_put(num);
	}

	return 0;
}

void get_input(int&num)
{
	cout<<"Enter integer greater than 1: " ;
	cin>>num;
	
	return;
}

bool divide(int num)
{ 
if (num<=1)
    return false;
	
else if ((num==2)||(num==3)||(num==5))
    return true;
	
else if ((num%5!=0)&&(num%3!=0)&&(num%2!=0))
    return true;

else
    return false;
}
void out_put(int num)
{
	if(divide(num))
	cout<<num<<" is prime"<<endl;
    else if(num==0)
	cout<<"done"<<endl;
	else
	cout<<num<<" is not prime"<<endl;
}
 
You can't go above a certain number before you start to run out of bytes to store your variable. Each primative data type gets only so much space to get stored in. How much depends on the language and compiler. Put the keyword "unsigned" in front if it's declaration and you will get more positive values to work with, but you won't be able to go negative. Putting "long" in front of it should get you more memory; using a different data type all together such as a float would give you even more memory.

Neat Fact: TI-C allows for "usigned long long ints" - a primative from 0-18446744073709551615
 
I haven't even looked at your code, save for the initial variable declaration. Assuming a 32-bit environment, and assuming some other things about your environment - as variable size in bytes isn't a part of the C++ standard - then the largest value is 4,294,967,296. You can increase the positive range by using unsigned int, but you may be better of using a long or unsigned long.

Check out this page for some common integer types in various languages, as well as the most commong size, in bits, associated with each integer type.
 
Trombe absolutely right.

you would need the following

The range of the long int types:
A signed long can hold all the values between LONG_MIN and LONG_MAX inclusive. LONG_MIN is required to be -2147483647 or less, LONG_MAX must be at least 2147483647. Again, many 2's complement implementations will define LONG_MIN to be -2147483648 but this is not required. An unsigned long can hold all the values between 0 and ULONG_MAX inclusive. ULONG_MAX must be at least 4294967295. The long types must contain at least 32 bits to hold the required range of values.
 
Thanks , using "usinged" and "long double" I was able to type very large numbers, but it seems to be rounding off which is useless for this.

Using "usinged" and "long" don't work and "long int" is the same as "int" .
 
Depending on the compiler, there is a "long long" which is 64bit. long double is a floatingpoint value of course which works totally different and is useless for prime testing. Another solutions might be a bignum library: not even the 64bit limitation anymore. Or you use a 64bit CPU under a 64bit OS.
 
Romulox said:
but it seems to be rounding off which is useless for this..

Did you change all of your "int" statements/declarations to use the bigger data type? You must change it in everything including the function parameters, etc.

I am almost positive that a long unsigned int on a 32 bit platform should be 64 bits. That would give it a range from 0 to 2^64 - 1 = (18446744073709551616 - 1) = 18446744073709551615.
 
Heres my experiece:
int 32-bit
long 32-bit
long int 32 bit

When you have long without a type it assumes int. I think this is in the standard so that long and long int are always the same (like short and short int).

gcc gives you a long long type, which I believe is 64 bit. The MS compiler will give you an __int64. These are both non-standard extensions.
 
Yea, on second thought I think mccoyn is right. The long was in place for 32-bit ints when standard ints were 16 bit. Int sizes all depended on the platform, it was hard to remember. I do most of my work in C# or java now so am starting to forget the exact details of C/C++.
 
You cannot use floating point for this (double).

The type "long long" will do what you want, any self-respected 32 bit Windows or Unix compiler should have 64 bit for it.

Of course that only shifts the problem up a few levels and you run into the same problem again. To avoid it you will have to use a library or a language that implements integers of arbitrary size.

BTW, C/C++'s "int" on AMD64/EM64T is 32 bits, not 64 bits.
 
On most compilers I've used, whatever you use, it'll be 32 bit. The difference between int/long and float/double disappeared a long time ago for most.

As mentioned above, there are a number of "bignum" or "bigint" libraries in C++ that allow you to create a data type that dynamically allocates memory and has no limit to size.

The other solution is to use a piece of software like mathematica which can work with number of any length. It has a programming language as well. The computations you'll get are probably a good deal faster than what you'll get with bignum.
 
Back