How to compare large integers represented as exponents

58 Views Asked by At

I have large integers that can only be factored into 2, 3 and 5, which are known as 5-smooth numbers. Since they are extremely large, I want to represent each such number as a triplet of exponents; for example, $360 = 2^33^25$ will be represented as $(3,2,1)$.

My question is: given 2 such triplets, how to compare them to determine which one is larger? If this is difficult to do, are there other ways to compare extremely large 5-smooth numbers?

2

There are 2 best solutions below

3
On

Compare the logarithm of their ratio:

$$\log \left( \dfrac{2^{a_1}3^{b_1}5^{c_1}}{2^{a_2}3^{b_2}5^{c_2}} \right) = (a_1-a_2) * \log 2 + (b_1-b_2) * \log 3 + (c_1-c_2) * \log 5 $$

So if you keep the values of the three logarithms around, you can just subtract the triples, take their weighted sum, and check if the final value is greater than or less than zero.

Also note that it doesn't matter what base you use for the logarithms, so you might as well use $\log_2$, as it eliminates one multiplication. And @RobertIsrael has given you some pointers in the comments on how you might want to "implement" the other two logs.

0
On

For example, suppose you want to compare $x = 2^{18109} 3^{13311} 5^{78533}$ and $y = 2^{1234} 3^{5678} 5^{91011}$. Thus $\log_2(x/y) = 16875 + 7633\; \log_2(3) - 12478 \; \log_2(5)$. Using the bounds in my comment, call them $a < \log_2(3) < b$ and $c < \log_2(5) < d$, and exact rational arithmetic $$ \log_2(x/y) \ge 16875 + 7633\; a - 12478\; d = \frac{10692336877226592511500615697352249069160249067552360957604649661900932431026239389}{137510856707153011098323838512520736495133097760788393116779975825724269815133543514519711204} > 0$$ so $x > y$.