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

Added Karatsuba Multiplication #2

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

Merged
shushrutsharma merged 2 commits into shushrutsharma:master from mihirs16:master
Jun 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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
44 changes: 44 additions & 0 deletions 04. Algorithms/Divide and Conquer/karatsuba_multiplication.py
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
'''
karatsuba multiplication (integer-wise)
to multiply two n-digit integers, the following formula holds:
=> (a * c * 10^n) + (b * d) + {[(b * c) + (a * d)] * 10^(n/2)}
where: a = first half of first integer
b = second half of first integer
c = first half of second integer
d = second half of second integer
this can further be used with recursion to implement divide-and-conquer
wherein each product (ac, bd, bc, ad) can be calculated using the same.

note: assuming n = even & n1 = n2
'''
def k_multiply(x, y):
if len(x) > 1 and len(y) > 1: # base condition: length of integers > 1
a, b = x[:len(x) // 2], x[len(x) // 2:] # divide first int for a, b,
c, d = y[:len(y) // 2], y[len(y) // 2:] # divide second int for c, d

print(a, b, c, d)
ac = k_multiply(a, c)
bd = k_multiply(b, d)
bc = k_multiply(b, c)
ad = k_multiply(a, d)
n = len(x)
n2 = n // 2
prod = (ac * pow(10, n)) + (bd) + ((bc + ad) * pow(10, n2)) # prod by karatsuba

return prod
else:
return (x[0] * y[0]) # return product if length <= 1

if __name__ == '__main__':
try:
first_integer = [int(x) for x in input('first integer: ')]
second_integer = [int(x) for x in input('second integer: ')]

if (len(second_integer) != len(first_integer)):
raise ValueError

prod = k_multiply(first_integer, second_integer)
print(prod)
except ValueError:
print('Invalid Inputs')

9 changes: 5 additions & 4 deletions readme.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,11 @@ All the essential resources and template code needed to understand and practice
7. [Trees](/03.%20Data%20Structures/Trees)

### 4. 🛠 [Algorithms](/04.%20Algorithms/)
1. [Dynamic Programming](/04.%20Algorithms/Dynamic%20Programming/)
2. [Recursion](/04.%20Algorithms/Recursion/)
3. [Sorting](/04.%20Algorithms/Sorting/)
4. [Traversals](/04.%20Algorithms/Traversals)
1. [Divide and Conquer](/04.%20Algorithms/Divide%20and%20Conquer/)
2. [Dynamic Programming](/04.%20Algorithms/Dynamic%20Programming/)
3. [Recursion](/04.%20Algorithms/Recursion/)
4. [Sorting](/04.%20Algorithms/Sorting/)
5. [Traversals](/04.%20Algorithms/Traversals)

### 5. 📂 [File Handling and OOPS](/05.%20File%20Handling%20and%20OOPS/)
1. [File + Classes Demo](/05.%20File%20Handling%20and%20OOPS/file%2Bclasses.py)
Expand Down

AltStyle によって変換されたページ (->オリジナル) /