Storing and printing integer values greater than 2^64
Asked Answered
C

6

9

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.

Cantina answered 24/3, 2013 at 17:9 Comment(2)
Note that you will not be able to use those C operators, you will have to call functions to do the mathematical operations. C does not support operator overloading.Brookbrooke
Yes, you will have no choice but to either use functions or go over to c++ where you could do that.Aweather
I
8

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.

Intemperate answered 24/3, 2013 at 17:14 Comment(0)
Y
6

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.

Yggdrasil answered 24/3, 2013 at 17:18 Comment(0)
A
4

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).

Aweather answered 24/3, 2013 at 17:22 Comment(0)
P
3

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.

Plasticity answered 24/3, 2013 at 17:15 Comment(0)
E
2

Take a look at the GNU MP Bignum Library.

Elisha answered 24/3, 2013 at 17:14 Comment(0)
O
-1

Use double :)

it will solve your problem!

Orchestrate answered 28/4, 2021 at 7:5 Comment(1)
I don't think using double will solve anything. If we assume typical system where long long and double are both 64-bits, then it will actually make things worse because double only has 53-bits of precision.Galloromance

© 2022 - 2024 — McMap. All rights reserved.