how do I declare an integer variable of 1024 bits in length? - c++

I'm trying to write an algorithm for a number theory/computer science merged class that can factor large numbers in better than exponential time. I am using the g++ compiler on a 64 bit machine but when I chain together long it will only allow me to do up to 2 longs. Is there any way to tell it to use an arbitrary amount of space for a variable?

If you want just a collection of longs, you can declare an array of longs. But you don't want that. You want https://mattmccutchen.net/bigint/ BigIntegers :-)
Alternatives:
http://gmplib.org/
http://www.mpir.org/
(disclaimer: I haven't tested/used them)
Or if you want to implement them
How to implement big int in C++
I'll add that the C++ std library doesn't contain a big integer implementation (source STL big int class implementation )

You'll need a library. A good one is http://gmplib.org/

Related

How to handle a integer value bigger than the max int(64) can store? [duplicate]

I am trying to write a program for finding Mersenne prime numbers. Using the unsigned long long type I was able to determine the value of the 9th Mersenne prime, which is (2^61)-1. For larger values I would need a data type that could store integer values greater than 2^64.
I should be able to use operators like *, *=, > ,< and % with this data type.
You can not do what you want with C natives types, however there are libraries that let handle arbitrarily large numbers, like the GNU Multiple Precision Arithmetic Library.
To store large numbers, there are many choices, which are given below in order of decreasing preferences:
1) Use third-party libraries developed by others on github, codeflex etc for your mentioned language, that is, C.
2) Switch to other languages like Python which has in-built large number processing capabilities, Java, which supports BigNum, or C++.
3) Develop your own data structures, may be in terms of strings (where 100 char length could refer to 100 decimal digits) with its custom operations like addition, subtraction, multiplication etc, just like complex number library in C++ were developed in this way. This choice could be meant for your research and educational purpose.
What all these people are basically saying is that the 64bit CPU will not be capable of adding those huge numbers with just an instruction but you rather need an algorithm that will be able to add those numbers. Such an algorithm would have to treat the 2 numbers in pieces.
And the libraries they listed will allow you to do that, a good exercise would be to develop one yourself (just the algorithm/function to learn how it's done).
There is no standard way for having data type greater than 64 bits. You should check the documentation of your systems, some of them define 128 bits integers. However, to really have flexible size integers, you should use an other representation, using an array for instance. Then, it's up to you to define the operators =, <, >, etc.
Fortunately, libraries such as GMP permits you to use arbitrary length integers.
Take a look at the GNU MP Bignum Library.
Use double :)
it will solve your problem!

The use and the idea of integer to string conversion

I try to handle with big numbers in C++. One thing that I tried is installing the gmp library but this is not working properly on my computer (see this post). So I want to try another method and that is integer to string conversion.
But I dont get the idea of that. Let me make myself clear. For example we handle with a big integer. Lets say 2^1000. When, for example, I want to calculate 2^1000 mod 10 this is not possible (so far I know) with the normal libraries of c++. So my question is: Is it possible when converting my integer to a string and if the answer is yes:
How can I do arithmetic operations when I convert my integer to a string.
If you are using c++ predefined integer type, then 2^1000 is simply impossible. On your system maximum should be 2^16 or 2^32, max 2^64 (for long long). If you wanted to do that, you need to use (or implement yourself - what I don't recommend) infinite-precision integers.
You can convert normal int to string very easily with
... = std::to_string(/*Your int*/);
If you meant you want to do something like this:
amazing_to_string_conversion(1000000000000000000000000000000000000000000000)
It's not possible in any C++ implementation. The very number constant can't exist in code, it will many, many times overflow.
And if you consider implementing it yourself, it will probably K.O. you, because of very complicated calculations during division and non-trivial calculations like sqrt().

How to store extremely large numbers?

For example I have a factorial program that needs to save really huge integers that can be 50+ digits long. The absolute maximum primitive data type in C++ is unsigned long long int with a maximum value 18446744073709551615 which is only 20 digits long. Here's the link to the limits of C++: http://www.cplusplus.com/reference/climits/
How do I store numbers that are larger than that in a variable of some sort?
If you already have a boost dependency (which many people these days do), you can use the boost multi-precision library. In fact, it already has an example of a factorial program that can support output up to 128 bits, though extending it further is pretty trivial.
You'll have to use a bigint or bignum implementation. There are some libraries out there like this: http://gmplib.org/
Some more info and a list of libraries: http://en.wikipedia.org/wiki/Bignum
You can use array. First you should copy that giant number array,then use comma after 19 digits:
unsigned long long int num[]= {
7316717653133062491,9225119674426574742,3553491949349698352,0,312774506326239578,3180169848018694788,
5184385861560789112,9494954595017379583,3195285320880551112,5406987471585238630,5071569329096329522,
7443043557668966489,5044524452316173185,6403098711121722383,1136222989342338030,8135336276614282806,
4444866452387493035,8907296290491560440,7723907138105158593,0,7960866701724271218,8399879790879227492,
1901699720888093776,6572733300105336788,1220235421809751254,5405947522435258490,7711670556013604839,
5864467063244157221,5539753697817977846,1740649551492908625,6932197846862248283,9722413756570560574,
9026140797296865241,4535100474821663704,8440319989000889524,3450658541227588666,8811642717147992444,
2928230863465674813,9191231628245861786,6458359124566529476,5456828489128831426,0,7690042242190226710,
5562632111110937054,4217506941658960408,0,7198403850962455444,3629812309878799272,4428490918884580156,
1660979191338754992,0,0,5240636899125607176,0,6058861164671094050,7754100225698315520,0,0,
5593572972571636269,5618826704282524836,0,0,8232575304207529634,50};
There are many ways to store very big number which are below:
string
File
Link list
Vector/Dynamic array
Note: Please don't use array to avoid memory problem issues.

Bit operations on an arbitrarily large bit-array or number

I have a very simple question: In C++, is there an in-built or straightforward way to group a large (~1000) number of bits (or bools) in a single label such that the inbuilt bit operators function as they do for fundamentals?
e.g. for a long you might write:
unsigned long maximum = ~0;
or one might use:
somenum>>;
Is there an analogous way to do this for a block of memory of arbitrary size?
If not, what are some good alternatives ? I have thought of bit <vectors>, a C union, etc., but these all seem to require handwritten routines for the various bit operations.
Yep! It's called std::bitset and does just that.
Hope this helps!
Also, boost::dynamic_bitset might be useful depending on the requirements. I wish it was standard instead of the hack that std::vector<bool> is

What data structure should I use for BigInt class

I would like to implement a BigInt class which will be able to handle really big numbers. I want only to add and multiply numbers, however the class should also handle negative numbers.
I wanted to represent the number as a string, but there is a big overhead with converting string to int and back for adding. I want to implement addition as on the high school, add corresponding order and if the result is bigger than 10, add the carry to next order.
Then I thought that it would be better to handle it as a array of unsigned long long int and keep the sign separated by bool. With this I'm afraid of size of the int, as C++ standard as far as I know guarantees only that int < float < double. Correct me if I'm wrong. So when I reach some number I should move in array forward and start adding number to the next array position.
Is there any data structure that is appropriate or better for this?
So, you want a dynamic array of integers of a well known size?
Sounds like vector<uint32_t> should work for you.
As you already found out, you will need to use specific types in your platform (or the language if you have C++11) that have a fixed size. A common implementation of big number would use 32bit integers and ensure that only the lower 16 bits are set. This enables you to operate on the digits (where digit would be [0..2^16) ) and then normalize the result by applying the carry-overs.
On a modern, 64-bit x86 platform, the best approach is probably to store your bigint as a dynamically-allocated array of unsigned 32-bit integers, so your arithmetic can fit in 64 bits. You can handle your sign separately, as a member variable of the class, or you can use 2's-complement arithmetic (which is how signed int's are typically represented).
The standard C <stdint.h> include file defines uint32_t and uint64_t, so you can avoid platform-dependent integer types. Or, (if your platform doesn't provide these), you can improvise and define this kind of thing yourself -- preferably in a separate "platform_dependent.h" file...