Say we have two numbers (not necessarily integers) x1
and x2
. Say, the user inputs a number y
. What I want to find, is a number y'
close to y
so that x1 % y'
and x2 % y'
are very small (smaller than 0.02
, for example, but lets call this number LIMIT
). In other words, I don't need an optimal algorithm, but a good approximation.
I thank you all for your time and effort, that's really kind!
Let me explain what the problem is in my application : say, a screen size is given, with a width of screenWidth
and a height of screenHeight
(in pixels). I fill the screen with squares of a length y'
. Say, the user wants the square size to be y
. If y
is not a divisor of screenWidth
and/or screenHeight
, there will be non-used space at the sides of the screen, not big enough to fit squares. If that non-used space is small (e.g. one row of pixels), it's not that bad, but if it's not, it won't look good. How can I find common divisors of screenWidth
and screenHeight
?
Given an integer num, find the closest two integers in absolute difference whose product equals num + 1 or num + 2. Return the two integers in any order. Explanation: For num + 1 = 9, the closest divisors are 3 & 3, for num + 2 = 10, the closest divisors are 2 & 5, hence 3 & 3 is chosen.
GCD of two numbers is the largest number that divides both of them. A simple way to find GCD is to factorize both numbers and multiply common prime factors.
The theorem states that if we divide two integers by their greatest common divisor, then the outcome is a couple of integers that are relatively prime. If (a,b)=d then (a/d,b/d)=1.
Finding Greatest Common Divisor by LCM MethodStep 1: Find the product of a and b. Step 2: Find the least common multiple (LCM) of a and b. Step 3: Divide the values obtained in Step 1 and Step 2. Step 4: The obtained value after division is the greatest common divisor of (a, b).
I don't see how you can ensure that x1%y' and x2%y' are both below some value - if x1 is prime, nothing is going to be below your limit (if the limit is below 1) except x1 (or very close) and 1.
So the only answer that always works is the trivial y'=1.
If you are permitting non-integer divisors, then just pick y'=1/(x1*x2), since the remainder is always 0.
Without restricting the common divisor to integers, it can be anything, and the whole 'greatest common divisor' concept goes out the window.
x1
and x2
are not very large, so a simple brute force algorithm should be good enough.
Divide x1
and x2
to y
and compute floor and ceiling of the results. This gives four numbers: x1f
, x1c
, y1f
, y1c
.
Choose one of these numbers, closest to the exact value of x1/y
(for x1f
, x1c
) or x2/y
(for y1f
, y1c
). Let it be x1f
, for example. Set y' = x1/x1f
. If both x1%y'
and y1%y'
are not greater than limit
, success (y'
is the best approximation). Otherwise add x1f - 1
to the pool of four numbers (or y1f - 1
, or x1c + 1
, or y1c + 1
), choose another closest number and repeat.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With