Skip to main content

Posts

Showing posts from March, 2022

Find the Closest Pair of Factors for a Given Number

Find the closest pair of factors for a given number. More formally, given a number n , write it as a product of two numbers a and b such that | a - b | is the minimum of all combinations. Example For example, given a number  812848 , it can be written as the product of two numbers in the following different ways: 1 × 812848 = 812,848 2 × 406424 = 812,848 4 × 203212 = 812,848 8 × 101606 = 812,848 16 × 50803 = 812,848 101 × 8048 = 812,848 202 × 4024 = 812,848 404 × 2012 = 812,848 503 × 1616 = 812,848 808 × 1006 = 812,848 Our required answer is the last product (808 × 1006) . abs(808 - 1006) = 198 is the smallest difference among all other combinations. Approach Regardless of whether n is a perfect square, if we try to find its square root, we obtain a real root for all n ∈ R. Only considering the integer part of the square root, we can then iterate by increasing upon it with a step difference of 1 until we find a value for when the product equals given n . So, every time w...