Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit56513cb

Browse files
junthbasnetpoyea
authored andcommitted
add-binary-exponentiation (TheAlgorithms#790)
1 parent30a3582 commit56513cb

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

‎maths/BinaryExponentiation.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#Author : Junth Basnet
2+
#Time Complexity : O(logn)
3+
4+
defbinary_exponentiation(a,n):
5+
6+
if (n==0):
7+
return1
8+
9+
elif (n%2==1):
10+
returnbinary_exponentiation(a,n-1)*a
11+
12+
else:
13+
b=binary_exponentiation(a,n/2)
14+
returnb*b
15+
16+
17+
try:
18+
base=int(input('Enter Base : '))
19+
power=int(input("Enter Power : "))
20+
exceptValueError:
21+
print ("Invalid literal for integer")
22+
23+
result=binary_exponentiation(base,power)
24+
print("{}^({}) : {}".format(base,power,result))
25+

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp