Program to find modulus of a number by concatenating n times in Python

Suppose we have a number A. We have to generate a large number X by concatenating A n times in a row and find the value of X modulo m.

So, if the input is like A = 15, n = 3, m = 8, then the output will be 3, because the number X will be 151515, and 151515 mod 8 = 3.

Algorithm

To solve this, we will follow these steps ?

  • If A is same as 0, then return 0
  • Calculate the number of digits in A
  • Use modular arithmetic to avoid large number computations
  • Apply the geometric series formula for concatenated numbers
  • Return the final modulus result

How It Works

When we concatenate a number A n times, we get:

  • A concatenated 1 time: A
  • A concatenated 2 times: A × 10^c + A (where c is digits in A)
  • A concatenated 3 times: A × 10^(2c) + A × 10^c + A
  • Pattern: A × (10^((n-1)c) + 10^((n-2)c) + ... + 10^c + 1)

This forms a geometric series that we can solve using modular arithmetic.

Example

Let us see the following implementation to get better understanding ?

def solve(A, n, m):
    if A == 0:
        return 0
    
    # Store original A
    an = A
    
    # Count digits in A
    c = len(str(A))
    
    # Calculate 10^c (multiplier for concatenation)
    c = 10 ** c
    
    # d = c - 1, used for geometric series
    d = c - 1
    
    # New modulus to avoid overflow
    newmod = d * m
    
    # Calculate (c^n - 1) mod newmod using fast exponentiation
    val = pow(c, n, newmod) - 1
    val = (val + newmod) % newmod
    
    # Apply geometric series formula
    an = (an * val) % newmod
    
    # Return final result
    return an // d

# Test the function
A = 15
n = 3
m = 8
result = solve(A, n, m)
print(f"A = {A}, n = {n}, m = {m}")
print(f"Concatenated number: {str(A) * n}")
print(f"Result: {result}")
A = 15, n = 3, m = 8
Concatenated number: 151515
Result: 3

Step-by-Step Verification

Let's verify with our example manually ?

# Manual verification
A = 15
n = 3
m = 8

# Direct approach (works for small numbers)
concatenated = int(str(A) * n)
direct_result = concatenated % m

print(f"Direct concatenation: {concatenated}")
print(f"Direct modulus: {direct_result}")

# Our optimized approach
optimized_result = solve(A, n, m)
print(f"Optimized result: {optimized_result}")
print(f"Results match: {direct_result == optimized_result}")
Direct concatenation: 151515
Direct modulus: 3
Optimized result: 3
Results match: True

Key Points

  • Modular Arithmetic: Avoids integer overflow for large concatenated numbers
  • Geometric Series: The concatenated pattern follows a geometric progression
  • Fast Exponentiation: pow(c, n, newmod) efficiently computes large powers
  • Edge Case: Returns 0 when input number A is 0

Conclusion

This algorithm efficiently finds the modulus of concatenated numbers using modular arithmetic and geometric series properties. It avoids overflow issues that would occur with direct concatenation of large numbers.

Updated on: 2026-03-26T18:19:19+05:30

260 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements