Skip to content
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
add-binary-exponentiation
  • Loading branch information
junthbasnet committed May 7, 2019
commit 03297c8ea1acfcb825a715e1a8cee1bcfbeb33d6
25 changes: 25 additions & 0 deletions maths/BinaryExponentiation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#Author : Junth Basnet
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file already exists in maths. Please delete this.

#Time Complexity : O(logn)

def binary_exponentiation(a, n):

if (n == 0):
return 1

elif (n % 2 == 1):
return binary_exponentiation(a, n - 1) * a

else:
b = binary_exponentiation(a, n / 2)
return b * b


try:
base = int(input('Enter Base : '))
power = int(input("Enter Power : "))
except ValueError:
print ("Invalid literal for integer")

result = binary_exponentiation(base, power)
print("{}^({}) : {}".format(base, power, result))