Python Program for Given Two Numbers a and b Find all x Such that a % x = b

In the previous article, we have discussed Python Program for Modular Multiplicative Inverse from 1 to n

Given two numbers a, b and the task is to find all x such that given a % x = b

Examples:

Example1:

Input:

Given a value = 21
Given b value = 5

Output:

The value of x such that given a%x==b {a,b = 21 5 } =  2

Explanation:

Here the values of x which satisfy a%x=b are 8,16 because 21%8=5 ,21%16=5.
so total number of possible x are 8,16 i.e 2 values

Example2:

Input:

Given a value = 35
Given b value = 8

Output:

The value of x such that given a%x==b {a,b = 35 8 } =  2

Program for Given two Numbers a and b Find all x Such that a % x = b in Python

Below are the ways to find all x such that given a % x = b in python:

Method #1: Using For Loop (Static Input)

Approach:

  • Import the math module using the import keyword.
  • Give the number as static input and store it in a variable.
  • Give the other number as static input and store it in another variable.
  • Pass the given number two numbers as the arguments to the a_mod_xisb function.
  • Create a function to say a_mod_xisb which takes the given two numbers as the arguments and returns all the values of x such that given a % x = b.
  • Check if the given number a is less than the given b value using the if conditional statement.
  • If it is true then print “There are no solutions possible”.
  • Return.
  • Check if the given a value is equal to the given b value using the if conditional statement.
  • If it is true then print “Infinite Solutions are possible for the equation”.
  • Return.
  • Take a variable say cnt and initialize its value to 0.
  • Subtract the given b value from the given a value and store it in another variable say rslt.
  • Calculate the value of square root of (gvn_a_val – gvn_b_val) using the math.sqrt() function and convert result to an integer using the int() function.
  • Store it in another variable say k.
  • Loop from 1 to the above result k using the for loop.
  • Inside the loop, check if the above value of rslt modulus iterator value is equal to 0 using the if conditional statement.
  • Again check if the rslt divided by the iterator value greater than the given b value using the if conditional statement.
  • If it is true, increment the count value by 1 and store it in the same variable.
  • Check if the iterator value is greater than the given b value using the if conditional statement.
  • If it is true, increment the count value by 1 and store it in the same variable.
  • Check if the k multiplied with itself is equal to the rslt and k greater than the given b value using the if conditional statement.
  • If it is true, decrement the count value by 1 and store it in the same variable.
  • Print the value of x such that given a%x==b.
  • The Exit of the Program.

Below is the implementation:

# Import the math module using the import keyword.
import math

# Create a function to say a_mod_xisb which takes the given two numbers as the arguments
# and returns all the values of x such that given a % x = b.


def a_mod_xisb(gvn_a_val, gvn_b_val):
    # Check if the given number a is less than the given b value using the if conditional
    # statement.

    if (gvn_a_val < gvn_b_val):
      # If it is true then print "There are no solutions possible".
        print("There are no solutions possible")
        # Return.
        return
    # Check if the given a value is equal to the given b value using the if conditional
    # statement.
    if (gvn_a_val == gvn_b_val):
        # If it is true then print "Infinite Solutions are possible for the equation".
        # Return.
        print("Infinite Solutions are possible for the equation")
        return
    # Take a variable say cnt and initialize its value to 0.
    cnt = 0
    # Subtract the given b value from the given a value and store it in another variable
    # say rslt.

    rslt = gvn_a_val - gvn_b_val
    # Calculate the value of square root of (gvn_a_val - gvn_b_val) using the math.sqrt()
    # function and convert result to an integer using the int() function.
    # Store it in another variable say k.
    k = (int)(math.sqrt(gvn_a_val - gvn_b_val))
    # Loop from 1 to the above result k using the for loop.
    for itr in range(1, k+1):
      # Inside the loop, check if the above value of rslt modulus iterator value is equal
      # to 0 using the if conditional statement.
        if (rslt % itr == 0):
          # Again check if the rslt divided by the iterator value greater than the given b value
          # using the if conditional statement.
            if (rslt / itr > gvn_b_val):
              # If it is true, increment the count value by 1 and store it in the same variable.
                cnt = cnt + 1
      # Check if the iterator value is greater than the given b value using the if
          # conditional statement.
            if (itr > gvn_b_val):
                # If it is true, increment the count value by 1 and store it in the same variable.
                cnt = cnt + 1
        # Check if the k multiplied with itself is equal to the rslt and k greater than the
        # given b value using the if conditional statement.
    if (k * k == rslt and k > gvn_b_val):
        # If it is true, decrement the count value by 1 and store it in the same variable.
        cnt = cnt - 1
    # Print the value of x such that given a%x==b.
    print(
        "The value of x such that given a%x==b {a,b =", gvn_a_val, gvn_b_val, "} = ", cnt)


# Give the number as static input and store it in a variable.
gvn_a_val = 15
# Give the other number as static input and store it in another variable.
gvn_b_val = 2
# Pass the given number two numbers as the arguments to the a_mod_xisb function.
a_mod_xisb(gvn_a_val, gvn_b_val)

Output:

The value of x such that given a%x==b {a,b = 15 2 } =  1

Method #2: Using For loop (User Input)

Approach:

  • Import the math module using the import keyword.
  • Give the number as user input using the int(input()) function and store it in a variable.
  • Give the other number as user input using the int(input()) function and store it in another variable.
  • Pass the given number two numbers as the arguments to the a_mod_xisb function.
  • Create a function to say a_mod_xisb which takes the given two numbers as the arguments and returns all the values of x such that given a % x = b.
  • Check if the given number a is less than the given b value using the if conditional statement.
  • If it is true then print “There are no solutions possible”.
  • Return.
  • Check if the given a value is equal to the given b value using the if conditional statement.
  • If it is true then print “Infinite Solutions are possible for the equation”.
  • Return.
  • Take a variable say cnt and initialize its value to 0.
  • Subtract the given b value from the given a value and store it in another variable say rslt.
  • Calculate the value of square root of (gvn_a_val – gvn_b_val) using the math.sqrt() function and convert result to an integer using the int() function.
  • Store it in another variable say k.
  • Loop from 1 to the above result k using the for loop.
  • Inside the loop, check if the above value of rslt modulus iterator value is equal to 0 using the if conditional statement.
  • Again check if the rslt divided by the iterator value greater than the given b value using the if conditional statement.
  • If it is true, increment the count value by 1 and store it in the same variable.
  • Check if the iterator value is greater than the given b value using the if conditional statement.
  • If it is true, increment the count value by 1 and store it in the same variable.
  • Check if the k multiplied with itself is equal to the rslt and k greater than the given b value using the if conditional statement.
  • If it is true, decrement the count value by 1 and store it in the same variable.
  • Print the value of x such that given a%x==b.
  • The Exit of the Program.

Below is the implementation:

# Import the math module using the import keyword.
import math

# Create a function to say a_mod_xisb which takes the given two numbers as the arguments
# and returns all the values of x such that given a % x = b.


def a_mod_xisb(gvn_a_val, gvn_b_val):
    # Check if the given number a is less than the given b value using the if conditional
    # statement.

    if (gvn_a_val < gvn_b_val):
      # If it is true then print "There are no solutions possible".
        print("There are no solutions possible")
        # Return.
        return
    # Check if the given a value is equal to the given b value using the if conditional
    # statement.
    if (gvn_a_val == gvn_b_val):
        # If it is true then print "Infinite Solutions are possible for the equation".
        # Return.
        print("Infinite Solutions are possible for the equation")
        return
    # Take a variable say cnt and initialize its value to 0.
    cnt = 0
    # Subtract the given b value from the given a value and store it in another variable
    # say rslt.

    rslt = gvn_a_val - gvn_b_val
    # Calculate the value of square root of (gvn_a_val - gvn_b_val) using the math.sqrt()
    # function and convert result to an integer using the int() function.
    # Store it in another variable say k.
    k = (int)(math.sqrt(gvn_a_val - gvn_b_val))
    # Loop from 1 to the above result k using the for loop.
    for itr in range(1, k+1):
      # Inside the loop, check if the above value of rslt modulus iterator value is equal
      # to 0 using the if conditional statement.
        if (rslt % itr == 0):
          # Again check if the rslt divided by the iterator value greater than the given b value
          # using the if conditional statement.
            if (rslt / itr > gvn_b_val):
              # If it is true, increment the count value by 1 and store it in the same variable.
                cnt = cnt + 1
      # Check if the iterator value is greater than the given b value using the if
          # conditional statement.
            if (itr > gvn_b_val):
                # If it is true, increment the count value by 1 and store it in the same variable.
                cnt = cnt + 1
        # Check if the k multiplied with itself is equal to the rslt and k greater than the
        # given b value using the if conditional statement.
    if (k * k == rslt and k > gvn_b_val):
        # If it is true, decrement the count value by 1 and store it in the same variable.
        cnt = cnt - 1
    # Print the value of x such that given a%x==b.
    print(
        "The value of x such that given a%x==b {a,b =", gvn_a_val, gvn_b_val, "} = ", cnt)


# Give the number as user input using the int(input()) function and
# store it in a variable.
gvn_a_val = int(input("Enter some random number = "))
# Give the other number as user input using the int(input()) function and
# store it in another variable.
gvn_b_val = int(input("Enter some random number = "))
# Pass the given number two numbers as the arguments to the a_mod_xisb function.
a_mod_xisb(gvn_a_val, gvn_b_val)

Output:

Enter some random number = 35
Enter some random number = 8
The value of x such that given a%x==b {a,b = 35 8 } = 2

Explore more Example Python Programs with output and explanation and practice them for your interviews, assignments and stand out from the rest of the crowd.