# Find the larger exponential among two exponentials

Given four integers **A**, **B**, **C** and **D**. The task is to find which is greater **A ^{B}** or

**C**.

^{D}**Examples:**

Input:A = 2, B = 5, C = 4, D = 2

Output:2^5

2^{5}= 32

4^{2}= 16

Input:A = 8, B = 29, C = 60, D = 59

Output:60^59

**Naive approach:** Calculate the values of **A ^{B}** and

**C**then compare them. This approach will fail when the values are greater say

^{D}**562145**.

^{321457}**Efficient approach:** Using log, we can write the terms as **log(A ^{B})** and

**log(C**which can also be written as

^{D})**B * log(A)**and

**D * log(C)**. These values are easier to calculate and compare than the original values.

Below is the implementation of the above approach:

## C++

`// C++ implementation of the approach ` ` ` `#include<bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to find whether a^b is greater or c^d ` `void` `compareValues(` `int` `a, ` `int` `b, ` `int` `c, ` `int` `d) ` `{ ` ` ` ` ` `// Find b * log(a) ` ` ` `double` `log1 = ` `log10` `(a); ` ` ` `double` `num1 = log1 * b; ` ` ` ` ` `// Find d * log(c) ` ` ` `double` `log2 = ` `log10` `(c); ` ` ` `double` `num2 = log2 * d; ` ` ` ` ` `// Compare both values ` ` ` `if` `(num1 > num2) ` ` ` `cout << a << ` `"^"` `<< b; ` ` ` `else` ` ` `cout << c << ` `"^"` `<< d; ` `} ` ` ` `// Driver code ` `int` `main () ` `{ ` ` ` `int` `a = 8, b = 29, c = 60, d = 59; ` ` ` `compareValues(a, b, c, d); ` `} ` ` ` ` ` `// This code is contributed by ihritik ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the approach ` `class` `GFG { ` ` ` ` ` `// Function to find whether a^b is greater or c^d ` ` ` `static` `void` `compareValues(` `int` `a, ` `int` `b, ` `int` `c, ` `int` `d) ` ` ` `{ ` ` ` ` ` `// Find b * log(a) ` ` ` `double` `log1 = Math.log10(a); ` ` ` `double` `num1 = log1 * b; ` ` ` ` ` `// Find d * log(c) ` ` ` `double` `log2 = Math.log10(c); ` ` ` `double` `num2 = log2 * d; ` ` ` ` ` `// Compare both values ` ` ` `if` `(num1 > num2) ` ` ` `System.out.println(a + ` `"^"` `+ b); ` ` ` `else` ` ` `System.out.println(c + ` `"^"` `+ d); ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `main(String[] args) ` ` ` `{ ` ` ` `int` `a = ` `8` `, b = ` `29` `, c = ` `60` `, d = ` `59` `; ` ` ` `compareValues(a, b, c, d); ` ` ` `} ` `} ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation of the approach ` `import` `math ` ` ` `# Function to find whether ` `# a^b is greater or c^d ` `def` `compareValues(a, b, c, d): ` `# Find b * log(a) ` ` ` `log1 ` `=` `math.log10(a) ` ` ` `num1 ` `=` `log1 ` `*` `b ` ` ` ` ` `# Find d * log(c) ` ` ` `log2 ` `=` `math.log10(c) ` ` ` `num2 ` `=` `log2 ` `*` `d ` ` ` ` ` `# Compare both values ` ` ` `if` `num1 > num2 : ` ` ` `print` `(a, ` `'^'` `, b) ` ` ` `else` `: ` ` ` `print` `(c, ` `'^'` `, d) ` ` ` `# Driver code ` `a ` `=` `8` `b ` `=` `29` `c ` `=` `60` `d ` `=` `59` ` ` `# Function call ` `compareValues(a, b, c, d) ` ` ` `# This code is contributed by nidhiva ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the approach ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` ` ` `// Function to find whether ` ` ` `// a^b is greater or c^d ` ` ` `static` `void` `compareValues(` `int` `a, ` `int` `b, ` ` ` `int` `c, ` `int` `d) ` ` ` `{ ` ` ` ` ` `// Find b * log(a) ` ` ` `double` `log1 = Math.Log10(a); ` ` ` `double` `num1 = log1 * b; ` ` ` ` ` `// Find d * log(c) ` ` ` `double` `log2 = Math.Log10(c); ` ` ` `double` `num2 = log2 * d; ` ` ` ` ` `// Compare both values ` ` ` `if` `(num1 > num2) ` ` ` `Console.WriteLine(a + ` `"^"` `+ b); ` ` ` `else` ` ` `Console.WriteLine(c + ` `"^"` `+ d); ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `Main () ` ` ` `{ ` ` ` `int` `a = 8, b = 29, c = 60, d = 59; ` ` ` `compareValues(a, b, c, d); ` ` ` `} ` `} ` ` ` `// This code is contributed by ihritik ` |

*chevron_right*

*filter_none*

**Output:**

60^59

## Recommended Posts:

- Program to find whether a no is power of two
- Program to find parity
- Find minimum number to be divided to make a number a perfect square
- Find whether a given number is a power of 4 or not
- Find Union and Intersection of two unsorted arrays
- To find sum of two numbers without using any operator
- Find day of the week for a given date
- Given a number, find the next smallest palindrome
- Find the element that appears once
- Find the largest multiple of 2, 3 and 5
- Program to find amount of water in a given glass
- Find the maximum distance covered using n bikes
- Find if two rectangles overlap
- Find the smallest number whose digits multiply to a given number n
- Find next greater number with same set of digits

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.