Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Computing different terms for a given pair of exponentiation having the same result

To understand the problem,let us consider these examples first:

                                 46 = (22)6 = 212 = (23)4 = 84 = 163 = 4096.

Thus,we can say that 46,212,84 and 163 are same.

                                 273 = 39 = 19683

so, both 273 and 39 are identical.

Now the problem is, for any given pair of ab how to compute all others possible (if any)xy where, ab = xy.I am interested in an algorithm that can be efficiently implemented in C/C++.

For example:

If the inputs are like this:

4,6 desired output :(2,12),(8,4)

8,4 desired output :(2,12),(2,6)

27,3 desired output :(3,9)

12,6 desired output :(144,3),(1728,2)

7,5 desired output : No duplicate possible

like image 767
whacko__Cracko Avatar asked Jan 18 '26 06:01

whacko__Cracko


2 Answers

This is mostly a math problem. You can extract all the prime factors of a number, and you'll get a list of prime numbers and their exponents, i.e., 216000 = 26 * 33 * 53. Then take the GCD of the exponents: GCD(6,3,3) = 3. Divide the exponents by the GCD to get the smallest root of the number, 22 * 31 * 51 = 60. Then factor the GCD — factors of 3 are 1 and 3. There is one way to express the number as an integral power for each factor of the GCD. You can express it as (603)1 or (601)3.

EDIT: fixed math error.

like image 192
Dietrich Epp Avatar answered Jan 20 '26 18:01

Dietrich Epp


If integers is the only thing you're interested in, you could just start extracting integer roots from the target number, and checking if the result is an integer.

You even have a convenient stop condition - whenever the root is below 2 you can stop. That is, the algorithm:

  • Given a result
  • N <- 2
  • Compute Nth root.
    • If it's an integer: add to answers
    • If it's < 2, exit loop
  • N += 1, back to previous step

This algorithm will always terminate.

like image 37
Eli Bendersky Avatar answered Jan 20 '26 18:01

Eli Bendersky



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!