MathsNumber Theory

Big Number
calculator.

Add, multiply, divide or raise whole numbers of any length and keep every digit.

How it's worked out

Most calculators store a number as a 64-bit double. MDN gives the limit: a double holds every whole number up to 9,007,199,254,740,991, which is 253−12^{53} - 1. Past that it skips some, so 9,007,199,254,740,992 + 1 comes back unchanged. This page keeps each number as a list of digits, so the sum is 9,007,199,254,740,993.

Add, subtract and multiply carry digits by hand, as on paper, just with no limit on length.

Divide and mod give a remainder r with 0≤r<|B|0 \le r < |B|. So −17 divided by 5 gives a quotient of −4 and a remainder of 3, and −17 mod 5 is 3.

A\displaystyle A=q×B+r\displaystyle {}= q \times B + r

GCD uses the Euclidean algorithm: swap in the remainder until it is 0. LCM divides by the GCD first, so the working numbers stay small.

gcd⁡(a,b)\displaystyle \gcd(a, b)=gcd⁡(b, amodb)\displaystyle {}= \gcd(b,\ a \bmod b)
lcm⁡(a,b)\displaystyle \operatorname{lcm}(a, b)=|a|gcd⁡(a,b)×|b|\displaystyle {}= \frac{|a|}{\gcd(a, b)} \times |b|

Power multiplies by repeated squaring, with results capped at 50,000 digits. Modular power squares once for each binary digit of the exponent and reduces after every step, so an exponent with hundreds of digits is fine. Factorial runs up to 5,000 factorial, and integer square root gives the largest whole r with r2≤Ar^2 \le A.

Each answer is checked by a second route. A product is divided back by B. A power is tested by casting out nines. A factorial has one trailing zero for each factor of 5, which is Legendre's count.

zeros of n!\displaystyle \text{zeros of } n!=⌊n5⌋+⌊n25⌋+⋯\displaystyle {}= \left\lfloor \frac{n}{5} \right\rfloor + \left\lfloor \frac{n}{25} \right\rfloor + \cdots

Worked examples

Priya finds where her spreadsheet goes wrong

Priya is testing IDs that pass through JavaScript. She adds 1 to 9,007,199,254,740,992, which is 2532^{53}. Here the sum is 9,007,199,254,740,993, and the check takes 1 back off to get the start again.

A double cannot hold 9,007,199,254,740,993, since it needs 54 bits and a double keeps 53. So her code rounds it to 9,007,199,254,740,992. She stores the IDs as text or as BigInt instead.

Open this example in the tool

Marco counts the zeros at the end of 100 factorial

For a puzzle, Marco wants the trailing zeros of 100!100!. The result has 158 digits and begins 933262154439. It ends in 24 zeros.

The check counts factors of 5: 20 + 4 = 24. Each 5 pairs with a 2 to make a 10, and 2s are far more common, so the 5s set the count. The extra 4 come from 25, 50, 75 and 100, which hold two 5s each.

Open this example in the tool

Lena checks a modular power from a cryptography class

Lena needs 7655377^{65537} mod 1,000,000,007. The full power would run to tens of thousands of digits, but reducing after each squaring gives 95,353,359 at once. The exponent 65537 is 216+12^{16} + 1, which has 17 binary digits, so it takes 17 squarings.

The check works the half power, 7327687^{32768} mod 1,000,000,007 = 14,134,088. Squaring that and multiplying by 7 once more, mod 1,000,000,007, gives 95,353,359 again.

Open this example in the tool

Reading your result

The short form and every digit. A long answer shows first in scientific form, with 4, 6, 10 or 15 significant figures. The full value is always there too. The product of the two 30-digit defaults has 60 digits and 197 bits.

The two size limits. "Exact in a double" asks if the result is at most 253−12^{53} - 1. "Fits a signed 64-bit integer" asks if it is between −263-2^{63} and 263−12^{63} - 1, the range of a long in Java or an int64 in C. The largest that fits is 9,223,372,036,854,775,807, and one more does not.

The prime test. It runs Miller-Rabin with the first 13 primes as bases. OEIS A014233 lists the smallest number that fools each set of bases, and with 13 bases that is 3,317,044,064,679,887,385,961,981. Below it, a pass is a proof. Above it, a pass is called a probable prime, while a fail always proves the number composite. With only 12 bases, 318,665,857,834,031,151,167,461 would slip through; here it is reported as composite.

Digit sum. A number is divisible by 9 when its digit sum is, and by 3 the same way. The digits of 2642^{64} add to 88, which leaves 7 after dividing by 9, so 2642^{64} is not a multiple of 3. The digit chart shows how often each digit appears, which is a quick way to spot a pattern such as the long run of zeros at the end of a factorial.

Common mistakes

  • Trusting the last digits from a phone or a spreadsheet. Past 15 or 16 significant digits, a double rounds. The leading digits look right, which hides the error.
  • Expecting −17 mod 5 to be −2. The % sign in C, Java and JavaScript keeps the sign of A, so it gives −2. Number theory, and this page, use the residue from 0 to B − 1, which is 3.
  • Typing 1e9 or 2.5. Exponent notation and decimals are refused rather than rounded. Type 1000000000, or work out a power with the Power operation.
  • Reading a probable prime as proven. Above the bound in OEIS A014233 a pass is strong evidence, not a proof. A result over 300 digits is not tested at all.
  • Building a huge power to take a remainder. ABA^B mod C does not need ABA^B. Use Modular power: 2 to the power 1,000,000,006, mod 1,000,000,007, comes out as 1 straight away.

Questions people ask

They store a big number as a list of smaller pieces and carry between them, much like long multiplication on paper. Python, Java's BigInteger and JavaScript's BigInt all work this way. The cost grows with the length, so a 1,000-digit product takes longer than a 10-digit one, but no digit is lost.

Sources