Given two positive integers n and k. Find minimum positive integer x such that the (x % k) * (x / k) == n, where % is the modulus operator and / is the integer division operator.
Input : n = 4, k = 6
Explanation : (10 % 6) * (10 / 6) = (4) * (1) = 4 which is equal to n
Input : n = 5, k = 5
Output : 26
Approach: The problem has been solved using the value of K in the Set-1. In this article, we will use the factor method to solve the above problem. Given below are the steps to solve the above problem.
- Since the equation is of the form a*b = N, so a and b will be the factors of N.
- Iterate from 1 to sqrt(N) to get all the factors.
- The factors i and n/i can be either of A or B.
- If i is A and n/i is B then the number will be i*k + (n/i). We can check if this number is the one by comparing it with N, if it is then it is a number which satisfies the equation.
- If i is B and n/i is A then the number will be (n/i)*k + (i). We can check if this number is the one by comparing it with N, if it is then it is a number which satisfies the equation.
- Obtain all the numbers which satisfies the equation and print the smallest among them.
Below is the implementation of the above approach.
# Python3 program to find the minimum
# positive X such that the given
# equation holds true
# This function gives the required
def minimumX(n, k):
mini = sys.maxsize
# Iterate for all the factors
i = 1
while i * i <= n : # Check if i is a factor if (n % i == 0) : fir = i sec = n // i num1 = fir * k + sec # Consider i to be A and n/i to be B res = (num1 // k) * (num1 % k) if (res == n): mini = min(num1, mini) num2 = sec * k + fir res = (num2 // k) * (num2 % k) # Consider i to be B and n/i to be A if (res == n): mini = min(num2, mini) i += 1 return mini # Driver Code if __name__ == "__main__": n = 4 k = 6 print (minimumX(n, k)) n = 5 k = 5 print (minimumX(n, k)) # This code is contributed by ita_c [tabby title = "C#"]
Time Complexity : O(sqrt(N)), where N is the given positive integer.
- Divide two integers without using multiplication, division and mod operator | Set2
- Find minimum x such that (x % k) * (x / k) == n
- Find minimum sum of factors of number
- Find the maximum possible value of the minimum value of modified array
- Find minimum area of rectangle with given set of coordinates
- Find minimum number of coins that make a given value
- Find minimum possible digit sum after adding a number d
- Find the minimum value of m that satisfies ax + by = m and all values after m also satisfy
- Find minimum speed to finish all Jobs
- Find element using minimum segments in Seven Segment Display
- Find minimum number of Log value needed to calculate Log upto N
- Find the minimum difference between Shifted tables of two numbers
- Find minimum moves to reach target on an infinite line
- Find minimum operations needed to make an Array beautiful
- Program to find minimum number of lectures to attend to maintain 75%