Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting factors of a number with least sum in O(1)

I'm trying to find the factor pair of a number with least sum in O(1).

Here is the explanation :

If number is 100. Then all the possible pairs are :

1    X  100
2    X  50
4    X  25
5    X  20
10   X  10
20   X  5
25   X  4
50   X  2
100  X  1

Here the pair with least sum is 10,10 which is clearly the middle one

Similarly if number is 12 then pairs are as follows

1  X  12
2  X  6
3  X  4
4  X  3
6  X  2
12 X  1

Here the required pair is either 3,4 or 4,3.

If a number has 'p' pairs then the required one is always ceil(p/2).

If the given number is a perfect square then the task is pretty simple.The pair would be just sqrt(number),sqrt(number).

If not then the pair would be either ceil(sqrt(number)),number/ceil(sqrt(number))

given that ceil(sqrt(number)) is a factor of number

or the immediate factor neighbour of sqrt(number):

For example consider '6'. 6 is not a perfect square.

ceil of sqrt(6) is 3 and 3 is a factor of 6.So the required pair is 3,6/3=2

Now consider 102. All pairs are :

1  *  102.0
2  *  51.0
3  *  34.0
6  *  17.0
17  *  6.0
34  *  3.0
51  *  2.0
102 *  1

The required pair in this is 17,6 or 6,17. Here ceil(sqrt(102)) is 11. The immediate factor neighbour of 11 is 17 or 6. Now this is what we actually find.

How do we find that immediate factor neighbour ?

Here is my O(n) implementation :

import math

l = []
n = int(input())
for i in range(1, n + 1):
    if n % i is 0:
        l.append(i)
middle = l[math.ceil(len(l) / 2)]
print("Required pair is ", middle, ",", n / middle)
like image 533
Bharat Avatar asked Aug 24 '17 07:08

Bharat


People also ask

How do you find the factors of an efficient number?

Best Approach: If you go through number theory, you will find an efficient way to find the number of factors. If we take a number, say in this case 30, then the prime factors of 30 will be 2, 3, 5 with count of each of these being 1 time, so total number of factors of 30 will be (1+1)*(1+1)*(1+1) = 8.

How do you find all the factors of a number?

Finding Factors QuicklyEstablish the number you want to find the factors of, for example 24. Find two more numbers that multiply to make 24. In this case, 1 x 24 = 2 x 12 = 3 x 8 = 4 x 6 = 24. This means the factors of 24 are 1, 2, 3, 4, 6, 8, 12 and 24.

What is the least factor of a number?

The least prime factor of an integer n is the smallest prime number that divides the number. The least prime factor of all even numbers is 2. A prime number is its own least prime factor (as well as its own greatest prime factor). Note: We need to print 1 for 1.


1 Answers

Here is the proof that finding the pair must at least be as hard as integer factoring (which implies there is no known O(1) algorithm):

If we start with a number N and get the pair with the smallest sum, as it was shown, the divisors are the closest to the sqrt(N) so there only 2 possibilities:
1. The pair is 1 - N which means N is a prime. Which is the trivial case.
2. We found some non-trivial divisor k. Which means we can apply the algorithm successively for k and N/k, eventually finding all the prime divisors efficiently.

like image 93
The Javatar Avatar answered Oct 17 '22 05:10

The Javatar