Skip to content
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

Add karatsuba algorithm #225

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

appleJax
Copy link
Contributor

@appleJax appleJax commented Oct 16, 2018

This PR adds the Karatsuba fast multiplication algorithm to the algorithms/math folder.

README.md

Karatsuba Multiplication

Karatsuba is a fast multiplication algorithm discovered by Anatoly Karatsuba in 1960. Given two n-digit numbers, the "grade-school" method of long multiplication has a time complexity of O(n2), whereas the karatsuba algorithm has a time complexity of O(n1.59).

Recursive Formula

x = 1234
y = 5678

karatsuba(x, y)
  1. Split each number into numbers with half as many digits
a = 12
b = 34

c = 56
d = 78
  1. Compute 3 subexpressions from the smaller numbers
  • ac = a * c
  • bd = b * d
  • abcd = (a + b) * (c + d)
  1. Combine subexpressions to calculate the product
A = ac * 10000
B = (abcd - ac - bd) * 100
C = bd

x * y = A + B + C

Note: The karatsuba algorithm can be applied recursively to calculate each product in the subexpressions. (a * c = karatsuba(a, c)). When the numbers get smaller than some arbitrary threshold, they are multiplied in the traditional way.

References

Stanford Algorithms (YouTube)
Wikipedia

@codecov
Copy link

codecov bot commented Oct 16, 2018

Codecov Report

Merging #225 into master will not change coverage.
The diff coverage is 100%.

Impacted file tree graph

@@          Coverage Diff          @@
##           master   #225   +/-   ##
=====================================
  Coverage     100%   100%           
=====================================
  Files         143    144    +1     
  Lines        2554   2569   +15     
  Branches      422    423    +1     
=====================================
+ Hits         2554   2569   +15
Impacted Files Coverage Δ
...orithms/math/karatsuba-multiplication/karatsuba.js 100% <100%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update fad170c...c4eead4. Read the comment docs.

@appleJax appleJax force-pushed the feature/algorithm/karatsuba branch from 8a53ea1 to c4eead4 Compare October 18, 2018 16:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant