4

How to store a huge number of nearly 100000 digits in C++?..

I have tried using long long int and long double int..Nothing worked for me..

Is there any other way to store such a huge number?

I wish to find the smallest palindrome larger than the given huge number.

nurettin
  • 9,834
  • 5
  • 53
  • 77

4 Answers4

6

Upon further clarification in the comments section:

Yes, you can represent your number as a std::string in C++.

Here you find code for incrementing a number represented as a string:

#include <string>
#include <iostream>
#include <ostream>

void increment_numerical_string(std::string& s)
{
    std::string::reverse_iterator iter = s.rbegin(), end = s.rend();
    int carry = 1;
    while (carry && iter != end)
    {
        int value = (*iter - '0') + carry;
        carry = (value / 10);
        *iter = '0' + (value % 10);
        ++iter;
    }
    if (carry)
        s.insert(0, "1");
}

int main()
{
    std::string big_number = "123456789012345678901234567899";
    std::cout << "before increment: " << big_number << "\n";
    increment_numerical_string(big_number);
    std::cout << "after increment:  " << big_number << "\n";
}

You can use this in a loop to increment your big number and check if the resulting string is a palindrome:

if( equal(s.begin(), s.begin() + s.size()/2, s.rbegin()) )
    std::cout << "is a palindrome.\n";
else
    std::cout << "is NOT a palindrome.\n";

Edit

I do not claim this to be an efficient and correct solution to the problem. It's just a representation and incrementing method for big numbers.

Community
  • 1
  • 1
nurettin
  • 9,834
  • 5
  • 53
  • 77
  • I was thinking of this solution that's why I ask it first in the comment. You got me here :( – mr5 Oct 24 '13 at 07:12
3

You're looking for a 'bignum' or 'biginteger' library.

OpenSSL provides such a library, but it can be hard to use at times. Matt McCutchen's library seems much friendlier from an API standpoint.

atomicinf
  • 3,286
  • 15
  • 17
2

The GNU MP Bignum Library is a good solution. It is mainly a C library but has an effective C++ wrapper (with easy access to underlying C structures when needed). It has integer, rational, and floating point support.

edA-qa mort-ora-y
  • 26,115
  • 34
  • 118
  • 238
-1

If you are feeling incredibly brave you could implement a long number as a linked list of individual integers. So that you could increment or also add, subtract, multiply and divide such numbers it would be a good idea to make a class and use operator overloading.

Lloyd Crawley
  • 606
  • 3
  • 11
  • I did say if he/she is feeling INCREDIBLY BRAVE. I don`t know why you have to minus one me. I bite my thumb at you sir/madam. – Lloyd Crawley Oct 25 '13 at 01:20
  • Because storing bigint as a *linked list* is one of the least efficient solutions possible. When accessing bigint, you'll want random/indexed access for internal array of data (you can work without it, but it'll definitely make your life easier). Which means deque or vector. In addition to that there's significant memory overhead for linked list, because you'll need one or two pointers per digit which will be up to 16 extra bytes per digit on 64bit platform. Using list isn't even necessary, because in arithmetic operations you never remove a digit in the middle . That's why you get -1. – SigTerm Oct 25 '13 at 09:32
  • I agree with everything you say. However, it is still a valid suggestion, efficient or not. – Lloyd Crawley Oct 26 '13 at 06:27