Find an integer pair in a range satisfying a specific ratio

961 Views Asked by At

I am trying to convert an image(width*height) to a smaller one whose width is between 45 and 55, keeping its aspect ratio as possible.

For example:

I have an original image whose size is 966*644, so its aspect ratio is 0.666...

And then, I want to convert this image to a smaller one whose width is between 45 and 55.

One possible solution is 50*33. Its ratio is 0.66. It is so close to the origin ratio but not enough.

The best-fit size is 51*34, because its aspect ratio is 0.666...

So..

Is there any formula or ideas to solve this problem?

I know, sometimes there is no integer solution. Then I want to get the best-fit integer pair.