-
-
Notifications
You must be signed in to change notification settings - Fork 45.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Concatenate algorithms #8103
Concatenate algorithms #8103
Changes from all commits
b7e876a
0b8f54a
1ef07fc
d93e168
453c637
10b43d5
95c5ec0
a8a6be5
448cd67
91f4af7
be3b3e8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,78 @@ | ||
"""Binary Exponentiation.""" | ||
def binary_exponentiation_multiplication(a, b): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It looks like this function calculates |
||
""" | ||
* Binary Exponentiation with Multiplication | ||
* This is a method to find a*b in a time complexity of O(log b) | ||
* This is one of the most commonly used methods of finding result of multiplication. | ||
* Also useful in cases where solution to (a*b)%c is required, | ||
* where a,b,c can be numbers over the computers calculation limits. | ||
* Done using iteration, can also be done using recursion | ||
|
||
# Author : Junth Basnet | ||
# Time Complexity : O(logn) | ||
* Let's say you need to calculate a * b | ||
* RULE 1 : a * b = (a+a) * (b/2) -- example : 4 * 4 = (4+4) * (4/2) = 8 * 2 | ||
* RULE 2 : IF b is ODD, then -- a * b = a + (a * (b - 1)) :: where (b - 1) is even. | ||
* Once b is even, repeat the process to get a * b | ||
* Repeat the process till b = 1 OR b = 0, because a*1 = a AND a*0 = 0 | ||
|
||
* @author chinmoy159 | ||
* @version 1.0 dated 10/08/2017 | ||
""" | ||
|
||
def binary_exponentiation(a, n): | ||
res = 0 | ||
while b > 0: | ||
if b & 1: | ||
res += a | ||
|
||
if n == 0: | ||
a += a | ||
b >>= 1 | ||
|
||
return res | ||
|
||
|
||
def binary_exponentiation_powers(a, b): | ||
""" | ||
* Binary Exponentiation for Powers | ||
* This is a method to find a^b in a time complexity of O(log b) | ||
* This is one of the most commonly used methods of finding powers. | ||
* Also useful in cases where solution to (a^b)%c is required, | ||
* where a,b,c can be numbers over the computers calculation limits. | ||
* Done using iteration, can also be done using recursion | ||
|
||
* Let's say you need to calculate a ^ b | ||
* RULE 1 : a ^ b = (a*a) ^ (b/2) -- example : 4 ^ 4 = (4*4) ^ (4/2) = 16 ^ 2 | ||
* RULE 2 : IF b is ODD, then -- a ^ b = a * (a ^ (b - 1)) :: where (b - 1) is even. | ||
* Once b is even, repeat the process to get a ^ b | ||
* Repeat the process till b = 1 OR b = 0, because a^1 = a AND a^0 = 1 | ||
|
||
* @author chinmoy159 | ||
* @version 1.0 dated 10/08/2017 | ||
""" | ||
|
||
res = 1 | ||
while b > 0: | ||
if b & 1: | ||
res *= a | ||
|
||
a *= a | ||
b >>= 1 | ||
|
||
return res | ||
|
||
|
||
def binary_exponentiation_recursion(a, b): | ||
"""Binary Exponentiation with recursion. | ||
|
||
* Time Complexity : O(logn) | ||
* @author : Junth Basnet | ||
""" | ||
|
||
if b == 0: | ||
return 1 | ||
|
||
elif n % 2 == 1: | ||
return binary_exponentiation(a, n - 1) * a | ||
elif b % 2 == 1: | ||
return binary_exponentiation_recursion(a, b - 1) * a | ||
|
||
else: | ||
b = binary_exponentiation(a, n / 2) | ||
return b * b | ||
return binary_exponentiation_recursion(a, b / 2) ** 2 | ||
|
||
|
||
if __name__ == "__main__": | ||
|
@@ -24,5 +82,5 @@ def binary_exponentiation(a, n): | |
except ValueError: | ||
print("Invalid literal for integer") | ||
|
||
RESULT = binary_exponentiation(BASE, POWER) | ||
RESULT = binary_exponentiation_recursion(BASE, POWER) | ||
print(f"{BASE}^({POWER}) : {RESULT}") |
This file was deleted.
This file was deleted.
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This function also appears to calculate (modular) multiplication rather than exponentiation, so I feel like this might also belong better in a separate file for binary multiplication