Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Storing and printing integer values greater than 2^64

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.

like image 310
user1918672 Avatar asked Mar 24 '13 17:03

user1918672


2 Answers

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.

like image 169
RAM Avatar answered Oct 13 '22 23:10

RAM


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.

like image 41
jbr Avatar answered Oct 13 '22 21:10

jbr