Skip to main content

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 we compute the integer square root, it's assumed to be one of the factors while the other factor would be the integer quotient. Both are updated on every iteration until we reach a point where their product equals n.

Implementation in Python

def closestPair(n):
    a = b = int(n ** 0.5)
    while (a * b) != n:
        a += 1
        b = n // a
    return a, b

Both factors are initialized as the integer of square root of the given number n. We run a while loop as long their product doesn't equal given n.
In every iteration, we increment one of the factors, say a, by 1 while assigning the other factor, say b, with the integer quotient of the most recent value of a. The loop surely ends when both a and b are factors of the given number n and returns them both.

In the case of a prime number, the value of a increments by 1 until it reaches the value of n itself while b (obviously) reaches 1. This is valid because prime numbers only have two factors (1 and itself).

Time Complexity
O(n)

Space Complexity
O(1)

Comments

Popular posts from this blog

Why the usage of Leather Jackets that keep you warm could be Paradoxical and Useless

Why the usage of Leather Jackets that keep you warm could be Paradoxical and Useless Leather is a really popular material used in various industries. Just thinking about how most of it is made got me stuck with an interesting theory. I'm not entirely sure this is accurate information, but it's just a possibility in the long run. Please give it a read! Extraction of raw material Leather is prepared from the raw-hide skin of different animals like buffalo, goat, cow, and sheep. It is recorded that from total leather exports 40 percent of buffalo and 30 percent of goat rawhide skins are used for leather. Apart from this, exotic leathers such as snake and alligator are also available. Real leather is made from the skins of dead animals (animal hide) and in many Asian countries, animals are skinned alive and doing so is not so uncommon to them. If they are fortunate enough to be killed first, they are often beaten to death, electrocuted, drowned or hanged. This also includes animal ...

There's always a photon striking back to the source

There's always a photon striking back to the source    Note: This is only a speculation. I don't claim that any of the following is scientifically proven. It's entirely based on my perception. Hence, I am open to any sort of suggestions, opinions and discussions.  Introduction to Photons We all have heard about long years of debates over light being a wave, a particle or both and what events led to the discovery of photons. Put simply, photons are the fundamental particles of light, which has particle nature and uniquely, wave nature as well. Such unique properties of light allows light to undergo phenomena like reflection, refraction,diffraction and diffusion. Great scientists spent years of research over these particles into contributing towards quantum physics (further : quantum particles ).    Photons are mostly of the visible portion of the electromagnetic spectrum. The deep and continuous study and research about these particles led to ...