Skip to content

Commit d276295

Browse files
Merge pull request #617 from speztra/power_py
Create power.py
2 parents ddf9679 + 9cfe6cd commit d276295

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

Python/power.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
def power(x, y):
2+
if(y == 0):
3+
return 1
4+
if(y < 0):
5+
n = (1/x) * power(x, (y+1)/2)
6+
return n*n
7+
if(y%2 == 0):
8+
m = power(x, y/2)
9+
return m*m
10+
else:
11+
return x * power(x, y-1)
12+
13+
def main():
14+
print("To calculate x^y ...\n")
15+
x = float(input("Please enter x: "))
16+
y = float(input("Please enter y: "))
17+
18+
if(x==0):
19+
if(y > 0):
20+
print(0)
21+
else:
22+
print("x^y is not defined\n")
23+
else:
24+
print(power(x,y))
25+
exit
26+
27+
if __name__ == '__main__':
28+
main()

0 commit comments

Comments
 (0)