{"id":20750,"date":"2021-09-30T11:30:20","date_gmt":"2021-09-30T06:00:20","guid":{"rendered":"https:\/\/python-programs.com\/?p=20750"},"modified":"2021-11-22T18:35:29","modified_gmt":"2021-11-22T13:05:29","slug":"python-program-for-given-two-numbers-a-and-b-find-all-x-such-that-a-x-b","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-for-given-two-numbers-a-and-b-find-all-x-such-that-a-x-b\/","title":{"rendered":"Python Program for Given Two Numbers a and b Find all x Such that a % x = b"},"content":{"rendered":"
In the previous article, we have discussed Python Program for Modular Multiplicative Inverse from 1 to n<\/a><\/p>\n Given two numbers a, b and the task is to find all x such that given a % x = b<\/p>\n Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Explanation:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Below are the ways to find all x such that given a % x = b in python:<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Explore more Example Python Programs<\/a> with output and explanation and practice them for your interviews, assignments and stand out from the rest of the crowd.<\/p>\n 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 …<\/p>\nGiven a value = 21\r\nGiven b value = 5<\/pre>\n
The value of x such that given a%x==b {a,b = 21 5 } = 2<\/pre>\n
Here the values of x which satisfy a%x=b are 8,16 because 21%8=5 ,21%16=5.\r\nso total number of possible x are 8,16 i.e 2 values<\/pre>\n
Given a value = 35\r\nGiven b value = 8<\/pre>\n
The value of x such that given a%x==b {a,b = 35 8 } = 2<\/pre>\n
Program for Given two Numbers a and b Find all x Such that a % x = b in Python<\/h2>\n
\n
Method #1: Using For Loop (Static Input)<\/h3>\n
\n
# Import the math module using the import keyword.\r\nimport math\r\n\r\n# Create a function to say a_mod_xisb which takes the given two numbers as the arguments\r\n# and returns all the values of x such that given a % x = b.\r\n\r\n\r\ndef a_mod_xisb(gvn_a_val, gvn_b_val):\r\n # Check if the given number a is less than the given b value using the if conditional\r\n # statement.\r\n\r\n if (gvn_a_val < gvn_b_val):\r\n # If it is true then print \"There are no solutions possible\".\r\n print(\"There are no solutions possible\")\r\n # Return.\r\n return\r\n # Check if the given a value is equal to the given b value using the if conditional\r\n # statement.\r\n if (gvn_a_val == gvn_b_val):\r\n # If it is true then print \"Infinite Solutions are possible for the equation\".\r\n # Return.\r\n print(\"Infinite Solutions are possible for the equation\")\r\n return\r\n # Take a variable say cnt and initialize its value to 0.\r\n cnt = 0\r\n # Subtract the given b value from the given a value and store it in another variable\r\n # say rslt.\r\n\r\n rslt = gvn_a_val - gvn_b_val\r\n # Calculate the value of square root of (gvn_a_val - gvn_b_val) using the math.sqrt()\r\n # function and convert result to an integer using the int() function.\r\n # Store it in another variable say k.\r\n k = (int)(math.sqrt(gvn_a_val - gvn_b_val))\r\n # Loop from 1 to the above result k using the for loop.\r\n for itr in range(1, k+1):\r\n # Inside the loop, check if the above value of rslt modulus iterator value is equal\r\n # to 0 using the if conditional statement.\r\n if (rslt % itr == 0):\r\n # Again check if the rslt divided by the iterator value greater than the given b value\r\n # using the if conditional statement.\r\n if (rslt \/ itr > gvn_b_val):\r\n # If it is true, increment the count value by 1 and store it in the same variable.\r\n cnt = cnt + 1\r\n # Check if the iterator value is greater than the\u00a0given b value using the if\r\n # conditional statement.\r\n if (itr > gvn_b_val):\r\n # If it is true, increment the count value by 1 and store it in the same variable.\r\n cnt = cnt + 1\r\n # Check if the k multiplied with itself is equal to the rslt and k greater than the\r\n # given b value\u00a0using the if conditional statement.\r\n if (k * k == rslt and k > gvn_b_val):\r\n # If it is true, decrement the count value by 1 and store it in the same variable.\r\n cnt = cnt - 1\r\n # Print the value of x such that given a%x==b.\r\n print(\r\n \"The value of x such that given a%x==b {a,b =\", gvn_a_val, gvn_b_val, \"} = \", cnt)\r\n\r\n\r\n# Give the number as static input and store it in a variable.\r\ngvn_a_val = 15\r\n# Give the other number as static input and store it in another variable.\r\ngvn_b_val = 2\r\n# Pass the given number two numbers as the arguments to the a_mod_xisb function.\r\na_mod_xisb(gvn_a_val, gvn_b_val)\r\n<\/pre>\n
The value of x such that given a%x==b {a,b = 15 2 } = 1<\/pre>\n
Method #2: Using For loop (User Input)<\/h3>\n
\n
# Import the math module using the import keyword.\r\nimport math\r\n\r\n# Create a function to say a_mod_xisb which takes the given two numbers as the arguments\r\n# and returns all the values of x such that given a % x = b.\r\n\r\n\r\ndef a_mod_xisb(gvn_a_val, gvn_b_val):\r\n # Check if the given number a is less than the given b value using the if conditional\r\n # statement.\r\n\r\n if (gvn_a_val < gvn_b_val):\r\n # If it is true then print \"There are no solutions possible\".\r\n print(\"There are no solutions possible\")\r\n # Return.\r\n return\r\n # Check if the given a value is equal to the given b value using the if conditional\r\n # statement.\r\n if (gvn_a_val == gvn_b_val):\r\n # If it is true then print \"Infinite Solutions are possible for the equation\".\r\n # Return.\r\n print(\"Infinite Solutions are possible for the equation\")\r\n return\r\n # Take a variable say cnt and initialize its value to 0.\r\n cnt = 0\r\n # Subtract the given b value from the given a value and store it in another variable\r\n # say rslt.\r\n\r\n rslt = gvn_a_val - gvn_b_val\r\n # Calculate the value of square root of (gvn_a_val - gvn_b_val) using the math.sqrt()\r\n # function and convert result to an integer using the int() function.\r\n # Store it in another variable say k.\r\n k = (int)(math.sqrt(gvn_a_val - gvn_b_val))\r\n # Loop from 1 to the above result k using the for loop.\r\n for itr in range(1, k+1):\r\n # Inside the loop, check if the above value of rslt modulus iterator value is equal\r\n # to 0 using the if conditional statement.\r\n if (rslt % itr == 0):\r\n # Again check if the rslt divided by the iterator value greater than the given b value\r\n # using the if conditional statement.\r\n if (rslt \/ itr > gvn_b_val):\r\n # If it is true, increment the count value by 1 and store it in the same variable.\r\n cnt = cnt + 1\r\n # Check if the iterator value is greater than the\u00a0given b value using the if\r\n # conditional statement.\r\n if (itr > gvn_b_val):\r\n # If it is true, increment the count value by 1 and store it in the same variable.\r\n cnt = cnt + 1\r\n # Check if the k multiplied with itself is equal to the rslt and k greater than the\r\n # given b value\u00a0using the if conditional statement.\r\n if (k * k == rslt and k > gvn_b_val):\r\n # If it is true, decrement the count value by 1 and store it in the same variable.\r\n cnt = cnt - 1\r\n # Print the value of x such that given a%x==b.\r\n print(\r\n \"The value of x such that given a%x==b {a,b =\", gvn_a_val, gvn_b_val, \"} = \", cnt)\r\n\r\n\r\n# Give the number as user input using the int(input()) function and\r\n# store it in a variable.\r\ngvn_a_val = int(input(\"Enter some random number = \"))\r\n# Give the other number as user input using the int(input()) function and\r\n# store it in another variable.\r\ngvn_b_val = int(input(\"Enter some random number = \"))\r\n# Pass the given number two numbers as the arguments to the a_mod_xisb function.\r\na_mod_xisb(gvn_a_val, gvn_b_val)\r\n<\/pre>\n
Enter some random number = 35\r\nEnter some random number = 8\r\nThe value of x such that given a%x==b {a,b = 35 8 } = 2<\/pre>\n
\n