Skip to content

Commit 7598dbd

Browse files
abhikmpstokhos
authored andcommitted
LargestOfVeryLargeNumbers (TheAlgorithms#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
1 parent c711b20 commit 7598dbd

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed
+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Author: Abhijeeth S
2+
3+
import math
4+
5+
6+
def res(x, y):
7+
if 0 not in (x, y):
8+
# We use the relation x^y = y*log10(x), where 10 is the base.
9+
return y * math.log10(x)
10+
else:
11+
if x == 0: # 0 raised to any number is 0
12+
return 0
13+
elif y == 0:
14+
return 1 # any number raised to 0 is 1
15+
16+
17+
if __name__ == "__main__": # Main function
18+
# Read two numbers from input and typecast them to int using map function.
19+
# Here x is the base and y is the power.
20+
prompt = "Enter the base and the power separated by a comma: "
21+
x1, y1 = map(int, input(prompt).split(","))
22+
x2, y2 = map(int, input(prompt).split(","))
23+
24+
# We find the log of each number, using the function res(), which takes two
25+
# arguments.
26+
res1 = res(x1, y1)
27+
res2 = res(x2, y2)
28+
29+
# We check for the largest number
30+
if res1 > res2:
31+
print("Largest number is", x1, "^", y1)
32+
elif res2 > res1:
33+
print("Largest number is", x2, "^", y2)
34+
else:
35+
print("Both are equal")

0 commit comments

Comments
 (0)