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
Open
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
Prev Previous commit
fix typos
appleJax committed Oct 18, 2018
commit c4eead424ab042441a662a62b91f516aa0da1a10
2 changes: 1 addition & 1 deletion src/algorithms/math/karatsuba-multiplication/README.md
Original file line number Diff line number Diff line change
@@ -27,7 +27,7 @@ d = 78

3. Combine subexpressions to calculate the product
```
A = ac * 1000
A = ac * 10000
B = (abcd - ac - bd) * 100
C = bd
6 changes: 3 additions & 3 deletions src/algorithms/math/karatsuba-multiplication/karatsuba.js
Original file line number Diff line number Diff line change
@@ -15,7 +15,7 @@ export default function karatsuba(x, y) {
// SCALE FACTOR:
// scaleFactor is used to split the numbers
// into smaller numbers for recursion.
// when combining the subcomputations back
// when combining the subexpressions back
// together, the scaleFactor is used to
// recreate the magnitude of the original numbers
const minDigits = Math.min(
@@ -48,14 +48,14 @@ export default function karatsuba(x, y) {
// d = 5678 - (56 * 100) = 5678 - 5600 = 78
const d = y - (c * scaleFactor);

// COMPUTE SUB-EXPRESSIONS:
// COMPUTE SUBEXPRESSIONS:
// since a + b is less than x, and c + d is less than y
// the recursion is guaranteed to reach the base case
const ac = karatsuba(a, c);
const bd = karatsuba(b, d);
const abcd = karatsuba(a + b, c + d);

// COMBINE SUB-EXPRESSIONS:
// COMBINE SUBEXPRESSIONS:
// since the scaleFactor was used to
// reduce the size of the components,
// the scaleFactor must be applied in reverse