Find the kth digit after the decimal point of a fraction a/b with a,b,k being very large integers less than 10e18
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
If a, b, and k can be extremely large, the naive long-division method is too slow because it needs k steps. For values near 10^18, an O(k) algorithm is not realistic.
The key observation is that the kth digit after the decimal point depends on a remainder that can be computed with modular arithmetic. Once you switch to modular exponentiation, the problem drops to O(log k).
The Core Formula
Let r = a mod b. The fractional digits of a / b depend only on r / b, because the integer part is irrelevant after the decimal point.
For the kth digit:
- Compute
x = (r * 10^(k - 1)) mod b - The answer is
floor(10 * x / b)
That works because each decimal step in long division multiplies the current remainder by 10, extracts one digit, and keeps the next remainder modulo b.
So instead of simulating every digit from 1 through k, you jump directly to the remainder just before digit k.
A Fast Python Implementation
Python is a good fit here because its integers are arbitrary precision and pow supports modular exponentiation directly:
For 1 / 7 = 0.142857..., the digits are:
- first digit
1 - second digit
4 - sixth digit
7
The function reaches the target digit without materializing the earlier digits one by one.
Why pow(10, k - 1, b) Matters
The call pow(10, k - 1, b) computes 10^(k - 1) mod b efficiently using repeated squaring. That is the difference between a usable algorithm and an impossible one.
If you tried to compute 10^(k - 1) as a full integer first, the number would be enormous. If you tried to simulate long division up to k, the loop count would be enormous. Modular exponentiation avoids both problems.
That is the standard trick whenever you need to jump to a far-off position in a repeating decimal process.
Handling Special Cases
A few edge cases are worth checking:
- If
a % b == 0, then the fraction has no fractional part and every decimal digit is0. - If
bhas only factors2and5, the decimal terminates. After the terminating point, all later digits are0. - If
ais larger thanb, that does not matter. The method begins witha % b, so the integer part is automatically ignored.
Here is a quick demonstration:
Complexity
The algorithm is efficient because:
- modulo reduction is constant-time relative to the formula structure
- modular exponentiation is
O(log k) - only a fixed number of integer variables are stored
That is a huge improvement over the direct long-division approach, which is O(k).
For massive k, that complexity improvement is the whole point of the method.
Common Pitfalls
- Simulating all digits one by one, which is too slow for very large
k. - Forgetting to reduce
amodulobbefore working on the fractional part. - Using floating point arithmetic, which loses precision and breaks completely for large inputs.
- Computing
10^(k - 1)as a full integer instead of using modular exponentiation. - Off-by-one errors between the first digit after the decimal point and zero-based indexing.
Summary
- The
kth digit after the decimal point can be computed directly with modular arithmetic. - Start with
r = a mod b, because only the remainder affects the fractional digits. - Use
pow(10, k - 1, b)to jump to the relevant remainder inO(log k)time. - The answer is
floor(10 * x / b)after the modular step. - Avoid floating point and naive long division for large inputs.

