5 Best Ways to Find Remainder After Dividing n Number of 1s by m in Python

πŸ’‘ Problem Formulation: We want to know the remainder of a number consisting of n consecutive 1’s when divided by another number m. For example, if n = 5 and m = 2, the number is 11111, and the remainder when dividing this number by 2 is 1.

Method 1: Brute Force String Conversion

This method involves creating a string with n consecutive 1’s and then converting this string into an integer before performing the modulus operation with m. It’s straightforward but can be inefficient for large numbers.

Here’s an example:

def find_remainder(n, m):
    number_of_ones = '1' * n
    remainder = int(number_of_ones) % m
    return remainder

print(find_remainder(5, 2))

Output: 1

This code snippet defines a function find_remainder that generates a string of 1’s with length n, converts it to an integer, and finds the remainder when divided by m. It’s a direct translation of the problem statement to code.

Method 2: Mathematical Approach

By recognizing that a number composed of n 1’s is equivalent to a geometric series, we can use a mathematical formula to calculate the remainder without generating the actual number.

Here’s an example:

def find_remainder(n, m):
    remainder = (10**n - 1)//9 % m
    return remainder

print(find_remainder(5, 2))

Output: 1

This code unleashes the power of mathematical insight to compute the remainder of a number made up entirely of n 1’s divided by m, using an algebraic formula derived from the sum of a geometric series.

Method 3: Iterative Remainder Calculation

The iterative method keeps adding 1’s and taking the modulo with m at each step to avoid large number computation.

Here’s an example:

def find_remainder(n, m):
    remainder = 0
    for i in range(n):
        remainder = (remainder * 10 + 1) % m
    return remainder

print(find_remainder(5, 2))

Output: 1

Instead of creating a massive number, this code progressively calculates the remainder by iterating n times, appending one more 1 with each iteration, and applying modulo operation.

Method 4: Use of pow Function

This approach utilizes Python’s built-in pow function for efficient exponentiation in modulo arithmetic, thereby calculating the remainder directly.

Here’s an example:

def find_remainder(n, m):
    remainder = (pow(10, n, m*9) - 1) // 9
    return remainder

print(find_remainder(5, 2))

Output: 1

The code snippet saves processing power by leveraging Python’s pow function that can raise a number to a power and take the modulo simultaneously, which is significantly faster for large numbers.

Bonus One-Liner Method 5: Lambda Expression

A concise, one-liner solution using a lambda function can also solve the problem efficiently, offering both brevity and speed.

Here’s an example:

find_remainder = lambda n, m: int('1' * n) % m

print(find_remainder(5, 2))

Output: 1

This one-liner involves a lambda expression that encapsulates the logic of creating a string containing n 1’s and computing its modulo m as the remainder. This method is elegant but might not be as readable for beginners.

Summary/Discussion

  • Method 1: Brute Force String Conversion. Easy to understand. Not scalable for very large n.
  • Method 2: Mathematical Approach. Efficient for any size of n. Requires understanding of geometric series.
  • Method 3: Iterative Remainder Calculation. Avoids large number calculations. More efficient and readable than method 1.
  • Method 4: Use of pow Function. Very efficient for very large numbers. Uses in-built Python functionality for heavy lifting.
  • Method 5: Lambda Expression. Compact and efficient. Less readable for those unfamiliar with lambda functions.