Given three integers a, b and n, the task is to find out greater value between an and bn.
Input: a = 3, b = 4, n = 5
Output: b^n is greater than a^n
Value of an is 243 and the value of bn is 1024.
So, bn is greater than an.
Input: a = -3, b = 2, n = 4
Output: a^n is greater than b^n
Value of an is 243 and the value of bn is 16.
So, an is greater than bn.
Basic Approach: For every value of a, b and n, calculate the values of an and bn. Then compare the result obtained and display the according to output.
The problem with this approach arises when there are large values of a, b and n. For large values of a, n, calculating an can exceed the limit of integer which will cause integer overflow.
Better approach is to check the value of n.
- If n is even then calculate the absolute value of a and b.
- If n is odd then take the given value as it is.
- Now check if a is equal to b. If yes, print 0.
- If a is greater than b, print 1.
- Otherwise, print 2.
# Python3 code for finding greater
# between the a^n and b^n
# Function to find the greater value
def findGreater(a, b, n):
# If n is even
if ((n & 1) > 0):
a = abs(a);
b = abs(b);
if (a == b):
print(“a^n is equal to b^n”);
elif (a > b):
print(“a^n is greater than b^n”);
print(“b^n is greater than a^n”);
# Driver code
a = 12;
b = 24;
n = 5;
findGreater(a, b, n);
# This code is contributed by mits
b^n is greater than a^n
- Find next greater number with same set of digits
- Find the Next perfect square greater than a given number
- Find minimum value to assign all array elements so that array product becomes greater
- Program to find sum of 1 + x/2! + x^2/3! +...+x^n/(n+1)!
- Program to find HCF iteratively
- Program to find the sum of a Series (1*1) + (2*2) + (3*3) + (4*4) + (5*5) + ... + (n*n)
- Program to find LCM of 2 numbers without using GCD
- Program to find Sum of the series 1*3 + 3*5 + ....
- Program to find GCD or HCF of two numbers
- Program to Find the value of cos(nΘ)
- Program to find the value of tan(nΘ)
- Program to find LCM of two numbers
- Program to find the value of sin(nΘ)
- Program to find sum of series 1 + 1/2 + 1/3 + 1/4 + .. + 1/n
- Program to find the sum of a Series 1/1! + 2/2! + 3/3! + 4/4! +.......+ n/n!
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.