So, I've made programs like Prime number searchers and such. But the problem is if I use an int or long int variable for the program I am limited by the variable size. I can't search through numbers larger than their memory size. So my question is: Is there a way to allocate memory to a single variable, NOT AN ARRAY, so I can make a variable as many bytes as I want?
It is possible to create a user-defined numeric type that has a larger range. (Internally, the implementation would use something like an array, but we need to concern ourselves with those details.)
For instance, using boost::multiprecision::cpp_int
1 2 3 4 5 6 7 8 9 10 11
#include <iostream>
#include <boost/multiprecision/cpp_int.hpp>
int main()
{
using bigint = boost::multiprecision::cpp_int ;
bigint i( "1234567890987654321" ) ;
std::cout << i * i * i * i << '\n' ;
}
Otherwise, you could go about creating a class or typedef for arbitrary-precision arithmetic (though there are plenty of good libraries for this already), in which you'll probably end up using an array/vector and having a LOT of overloaded operators. >_>
There was a thread about creating bignums on the forum some time ago. If you decide to implement your own, you may want to take a look at it: http://www.cplusplus.com/forum/lounge/32041/