How to check which is greater: a**b or b**a for big numbers?

planetp :

Say I have two numbers, a and bsuch that

1 <= a, b <= 10**9

How can I quickly check which is greater: a^b or b^a? Calculating a**b directly in Python is too slow.

yxor :

This is more of a mathematical question than a Python related question, but you can use math.log to find the b * math.log(a) and a * math.log(b) and compare them.

import math
a = 10
b = 9

if b * math.log(a) > a * math.log(b):
   print("a^b is greater than b^a")
else if b * math.log(a)< a * math.log(b):
   print("a^b is smaller than b^a")

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=3991&siteId=1