mirror of
https://hub.njuu.cf/TheAlgorithms/Python.git
synced 2023-10-11 13:06:12 +08:00
LargestOfVeryLargeNumbers (#818)
* LargestOfVeryLargeNumbers Finds the largest among two very large numbers of the form x^y. Numbers like 512^513 etc * Rename LargestOfVeryLargeNumbers to LargestOfVeryLargeNumbers.py * Input() statements have been indented. input() statements are indented under if __name__ == "__main__": * largest_of_very_large_numbers.py
This commit is contained in:
parent
a0817bdcf0
commit
7b2c954169
35
maths/largest_of_very_large_numbers.py
Normal file
35
maths/largest_of_very_large_numbers.py
Normal file
@ -0,0 +1,35 @@
|
||||
# Author: Abhijeeth S
|
||||
|
||||
import math
|
||||
|
||||
|
||||
def res(x, y):
|
||||
if 0 not in (x, y):
|
||||
# We use the relation x^y = y*log10(x), where 10 is the base.
|
||||
return y * math.log10(x)
|
||||
else:
|
||||
if x == 0: # 0 raised to any number is 0
|
||||
return 0
|
||||
elif y == 0:
|
||||
return 1 # any number raised to 0 is 1
|
||||
|
||||
|
||||
if __name__ == "__main__": # Main function
|
||||
# Read two numbers from input and typecast them to int using map function.
|
||||
# Here x is the base and y is the power.
|
||||
prompt = "Enter the base and the power separated by a comma: "
|
||||
x1, y1 = map(int, input(prompt).split(","))
|
||||
x2, y2 = map(int, input(prompt).split(","))
|
||||
|
||||
# We find the log of each number, using the function res(), which takes two
|
||||
# arguments.
|
||||
res1 = res(x1, y1)
|
||||
res2 = res(x2, y2)
|
||||
|
||||
# We check for the largest number
|
||||
if res1 > res2:
|
||||
print("Largest number is", x1, "^", y1)
|
||||
elif res2 > res1:
|
||||
print("Largest number is", x2, "^", y2)
|
||||
else:
|
||||
print("Both are equal")
|
Loading…
Reference in New Issue
Block a user