10^9 is way smaller than 2^32

So in your case, no need to use unsigned long long (it fits, yes), which is overkill and can lead to slower operation.

Use the proper type, normalized in stdint.h include: uint32_t or uint_least32_t (uint32_t vs uint_fast32_t vs uint_least32_t)

long is also guaranteed to be at least 32 bits, so it's a good & simple choice as well.

Answer from Jean-François Fabre on Stack Overflow
🌐
Quora
quora.com › What-is-the-range-of-a-long-long-int-and-an-int-in-powers-of-10
What is the range of a long long int and an int in powers of 10? - Quora
Answer (1 of 5): => for int range(+ve and -ve), you can write this range in between (—10^9 to +10^9). => for long long int range(+ve and -ve), the range is in between (—10^18 to +10^18).
Discussions

numerical - Any way faster than pow() to compute an integer power of 10 in C++? - Stack Overflow
Explore Stack Internal ... I know power of 2 can be implemented using << operator. What about power of 10? Like 10^5? Is there any way faster than pow(10,5) in C++? It is a pretty straight-forward computation by hand. But seems not easy for computers due to binary representation of the numbers... More on stackoverflow.com
🌐 stackoverflow.com
What is the range of int? - general - CodeChef Discuss
Should int be able to hold <10^9 values · I was solving a question but i got WA and then i got AC after changing all int’s to long long int. Can’t discuss the specific problem as it is in ongoing contest · The problem might have been caused maybe because you were performing an operation ... More on discuss.codechef.com
🌐 discuss.codechef.com
1
3
March 28, 2019
[C++] How should i work with 10^100 numbers ?
Just store each digit as a byte. Then implement the usual addition/multiplication algorithms. Division would be harder. More on reddit.com
🌐 r/learnprogramming
23
1
May 14, 2016
What is the range of a long long int and an int in powers of 10?
Login Ask & Answer Success Stories Paid Courses Free Classes Tuition & Classes Fees Write a Review Help Center ... What is the range of a long long int and an int in... ... Hi,Just think before going for power of 10, the number "9,223,372,036,854,775,807" - prime factorization, so you can't ... More on urbanpro.com
🌐 urbanpro.com
1
0
🌐
Stack Overflow
stackoverflow.com › questions › 59158061 › how-to-store-integer-of-range-10100-in-c
dynamic programming - How to Store integer of range 10^100 in C? - Stack Overflow
Here is a more theoretical exposition of the same idea. Integers up to 10^100 can be written in base-10 notation with 100 or fewer digits (well, 101 digits for the number 10^100 itself).
🌐
Codeforces
codeforces.com › blog › entry › 108170
When should I use long long int instead of int? - Codeforces
=> for int range (+ve and -ve), you can write this range in between (—10^9 to +10^9). => for long long int range (+ve and -ve), the range is in between (—10^18 to +10^18). +1 · Raihan_Sikdar · 3 years ago · 5 · Comments (4) Show archived ...
🌐
Tpoint Tech
tpointtech.com › range-of-int-in-c
Range of Int in C - Tpoint Tech
March 17, 2025 - In this article, we are going to discuss the range of int in C with programs.
🌐
Mathematics LibreTexts
math.libretexts.org › bookshelves › applied mathematics › contemporary mathematics (openstax) › 14: appendix
14.0: Integer Powers of 10 - Mathematics LibreTexts
January 2, 2025 - In the following table, there are several nonnegative integer powers of 10 that have been written as a product. Notice that higher exponents result in larger products. What do you notice about the number of zeros in the resulting product? That’s right! The number of zeros is the same as the power each time! The reciprocal of a number is 1 divided by that number. For example, the reciprocal of 10 is ... 10. Similarly, any expression with a negative exponent can be written with a positive exponent by taking the reciprocal.
🌐
Sololearn
sololearn.com › en › Discuss › 2340660 › which-datatype-is-to-be-used-for-handling-values-in-range-10-18-in-c
Which datatype is to be used for handling values in range 10^18 in c++? | Sololearn: Learn to code for FREE!
June 11, 2020 - For more details please check the link below. www.geeksforgeeks.org/advanced-c-boost-library/amp/ There is a rule by which you can calculate how many digit a data type can store and the logic is 2^10 = 10^3 (nearly) i.e as long long int is an ...
Top answer
1 of 13
44

Something like this:

int quick_pow10(int n)
{
    static int pow10[10] = {
        1, 10, 100, 1000, 10000, 
        100000, 1000000, 10000000, 100000000, 1000000000
    };

    return pow10[n]; 
}

Obviously, can do the same thing for long long.

This should be several times faster than any competing method. However, it is quite limited if you have lots of bases (although the number of values goes down quite dramatically with larger bases), so if there isn't a huge number of combinations, it's still doable.

As a comparison:

#include <iostream>
#include <cstdlib>
#include <cmath>

static int quick_pow10(int n)
{
    static int pow10[10] = {
        1, 10, 100, 1000, 10000, 
        100000, 1000000, 10000000, 100000000, 1000000000
    };

    return pow10[n]; 
}

static int integer_pow(int x, int n)
{
    int r = 1;
    while (n--)
       r *= x;

    return r; 
}

static int opt_int_pow(int n)
{
    int r = 1;
    const int x = 10;
    while (n)
    {
        if (n & 1) 
        {
           r *= x;
           n--;
        }
        else
        {
            r *= x * x;
            n -= 2;
        }
    }

    return r; 
}


int main(int argc, char **argv)
{
    long long sum = 0;
    int n = strtol(argv[1], 0, 0);
    const long outer_loops = 1000000000;

    if (argv[2][0] == 'a')
    {
        for(long i = 0; i < outer_loops / n; i++)
        {
            for(int j = 1; j < n+1; j++)
            {
                sum += quick_pow10(n);
            }
        }
    }
    if (argv[2][0] == 'b')
    {
        for(long i = 0; i < outer_loops / n; i++)
        {
            for(int j = 1; j < n+1; j++)
            {
                sum += integer_pow(10,n);
            }
        }
    }

    if (argv[2][0] == 'c')
    {
        for(long i = 0; i < outer_loops / n; i++)
        {
            for(int j = 1; j < n+1; j++)
            {
                sum += opt_int_pow(n);
            }
        }
    }

    std::cout << "sum=" << sum << std::endl;
    return 0;
}

Compiled with g++ 4.6.3, using -Wall -O2 -std=c++0x, gives the following results:

$ g++ -Wall -O2 -std=c++0x pow.cpp
$ time ./a.out 8 a
sum=100000000000000000

real    0m0.124s
user    0m0.119s
sys 0m0.004s
$ time ./a.out 8 b
sum=100000000000000000

real    0m7.502s
user    0m7.482s
sys 0m0.003s

$ time ./a.out 8 c
sum=100000000000000000

real    0m6.098s
user    0m6.077s
sys 0m0.002s

(I did have an option for using pow as well, but it took 1m22.56s when I first tried it, so I removed it when I decided to have optimised loop variant)

2 of 13
11

There are certainly ways to compute integral powers of 10 faster than using std::pow()! The first realization is that pow(x, n) can be implemented in O(log n) time. The next realization is that pow(x, 10) is the same as (x << 3) * (x << 1). Of course, the compiler knows the latter, i.e., when you are multiplying an integer by the integer constant 10, the compiler will do whatever is fastest to multiply by 10. Based on these two rules it is easy to create fast computations, even if x is a big integer type.

In case you are interested in games like this:

  1. A generic O(log n) version of power is discussed in Elements of Programming.
  2. Lots of interesting "tricks" with integers are discussed in Hacker's Delight.
Find elsewhere
🌐
Microsoft Learn
learn.microsoft.com › en-us › cpp › cpp › data-type-ranges
Data Type Ranges | Microsoft Learn
C/C++ in Visual Studio also supports sized integer types. For more information, see __int8, __int16, __int32, __int64 and Integer Limits. For more information about the restrictions of the sizes of each type, see Built-in types. The range of enumerated types varies depending on the language context and specified compiler flags.
🌐
Javatpoint
javatpoint.com › range-of-int-in-c
Range of Int in C - javatpoint
Range of Int in C with Tutorial, C language with programming examples for beginners and professionals covering concepts, c pointers, c structures, c union, c strings etc.
🌐
asawicki.info
asawicki.info › news_1660_how_to_check_if_an_integer_number_is_a_power_of_10
How to check if an integer number is a power of 10?
October 4, 2017 - int IsLog10_v4(int32_t x) { if(x ... the most efficient solution is to notice that there are only few possible powers of 10 in the range of 32-bit integers, so we can just enumerate them all....
🌐
CodeChef Discuss
discuss.codechef.com › general
What is the range of int? - general - CodeChef Discuss
March 28, 2019 - Should int be able to hold <10^9 values · I was solving a question but i got WA and then i got AC after changing all int’s to long long int. Can’t discuss the specific problem as it is in ongoing contest · The problem might have been caused maybe because you were performing an operation ...
🌐
element14 Community
community.element14.com › technologies › code_exchange › b › blog › posts › c-tutorial---data-types
C++ Tutorial - Data Types - element14 Community
February 13, 2013 - However, unlike scientific notation, the base for the exponent is 2 and not 10. Much like integers, they still have a certain range of values that they can store and take a certain number of bits of memory.
🌐
Reddit
reddit.com › r/learnprogramming › [c++] how should i work with 10^100 numbers ?
r/learnprogramming on Reddit: [C++] How should i work with 10^100 numbers ?
May 14, 2016 -

So, i went to a small competition my school organised and in one of the two problems i have been given i had 5 tests that had to be met, two with 232 - 1 (integer), one with 263 - 1 (i used long long double, it worked), and two with 10100 numbers.

Question is : How am i supposed to use these numbers if the largest numbers one can use in c++ are long long integers ? I have heard somebody mention writing the integer to a char array or something like that but i have no idea how you would create such a big array.

Edit : Problem : Given a number x, a number of n lines and 2 numbers a and b on each one of the n lines write a program to test ax+b=0 and return the line number where this condition is met.

This would be the rough equivalent of the problem (there was some roleplay, "NASA has given a number of n lines, bla,bla,bla" but this would be the "raw" version. As i said, a pretty simple problem but the 10100 part is problematic.

Edit 2 : Limitations - Dimension of source must be max. 20Kb and the running time must not exceed 0.1seconds

🌐
UrbanPro
urbanpro.com › c language › learn c language
What is the range of a long long int and... - UrbanPro
What is the range of a long long int and an int in... ... Hi,Just think before going for power of 10, the number "9,223,372,036,854,775,807" - prime factorization, so you can't achieve as per your need.
Top answer
1 of 11
163

The minimum ranges you can rely on are:

  • short int and int: -32,767 to 32,767
  • unsigned short int and unsigned int: 0 to 65,535
  • long int: -2,147,483,647 to 2,147,483,647
  • unsigned long int: 0 to 4,294,967,295

This means that no, long int cannot be relied upon to store any 10-digit number. However, a larger type, long long int, was introduced to C in C99 and C++ in C++11 (this type is also often supported as an extension by compilers built for older standards that did not include it). The minimum range for this type, if your compiler supports it, is:

  • long long int: -9,223,372,036,854,775,807 to 9,223,372,036,854,775,807
  • unsigned long long int: 0 to 18,446,744,073,709,551,615

So that type will be big enough (again, if you have it available).


A note for those who believe I've made a mistake with these lower bounds: the C requirements for the ranges are written to allow for ones' complement or sign-magnitude integer representations, where the lowest representable value and the highest representable value differ only in sign. It is also allowed to have a two's complement representation where the value with sign bit 1 and all value bits 0 is a trap representation rather than a legal value. In other words, int is not required to be able to represent the value -32,768.

2 of 11
36

The size of the numerical types is not defined in the C++ standard, although the minimum sizes are. The way to tell what size they are on your platform is to use numeric limits

For example, the maximum value for a int can be found by:

std::numeric_limits<int>::max();

Computers don't work in base 10, which means that the maximum value will be in the form of 2n-1 because of how the numbers of represent in memory. Take for example eight bits (1 byte)

  0100 1000

The right most bit (number) when set to 1 represents 20, the next bit 21, then 22 and so on until we get to the left most bit which if the number is unsigned represents 27.

So the number represents 26 + 23 = 64 + 8 = 72, because the 4th bit from the right and the 7th bit right the left are set.

If we set all values to 1:

11111111

The number is now (assuming unsigned)
128 + 64 + 32 + 16 + 8 + 4 + 2 + 1 = 255 = 28 - 1
And as we can see, that is the largest possible value that can be represented with 8 bits.

On my machine and int and a long are the same, each able to hold between -231 to 231 - 1. In my experience the most common size on modern 32 bit desktop machine.

🌐
GeeksforGeeks
geeksforgeeks.org › c++ › data-type-ranges-and-their-macros-in-c
Data Type Ranges and Their Macros in C++ - GeeksforGeeks
5 days ago - C++ offers the numeric_limits<> class template as a modern alternative to these macros. This template provides a more object-oriented approach for accessing data type limits. It is defined inside the <limits> header file. ... #include <iostream> #include <limits> using namespace std; int main() { // Displaying ranges with the help of macros cout << "short int ranges from: " << numeric_limits<short int>::min() << " to " << numeric_limits<short int>::max() << endl; cout << "\nint ranges from: " << numeric_limits<int>::min() << " to " << numeric_limits<int>::max() << endl; cout << "\nlong int ranges from: " << numeric_limits<long>::min() << " to " << numeric_limits<long>::max() << endl; cout << "\nfloat ranges from: " << numeric_limits<float>::min() << " to " << numeric_limits<float>::max() << endl; return 0; }