Skip to content

Add karatsubaMultiplication #159

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

Closed
wants to merge 3 commits into from
Closed
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
14 changes: 14 additions & 0 deletions src/algorithms/math/bits/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,19 @@ inverting all of the bits of the number and adding 1 to it.

> See `switchSign` function for further details.

#### Karatsuba Multiplication

Given two numbers in binary representation, karatsuba algorithm find the multiplication
in O(n^1.59) T.C.

This uses the fact that

``
AB = 22ceil(num / 2) A_1B_1 + 2ceil(num / 2) * [(A_1 + A_r)(B_1 + B_r) - A_1B_1 - A_rB_r] + A_rB_r
``

> See `karasubaMultiplication` function for further details.

#### Multiply Two Numbers

This method multiplies two integer numbers using bitwise operators.
Expand Down Expand Up @@ -143,6 +156,7 @@ Count of Bits to be Flipped: 1

> See `bitsDiff` function for further details.


## References

- [Bit Manipulation on YouTube](https://www.youtube.com/watch?v=NLKQEOgBAnw&t=0s&index=28&list=PLLXdhg_r2hKA7DPDsunoDZ-Z769jWn4R8)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import karatsubaMultiplication from '../karatsubaMultiplication';

describe('karatsubaMultiplication', () => {
it('should multiply two numbers using karatsuba multiplication', () => {
const A = 1234;
const B = 5678;
expect(karatsubaMultiplication(A, B)).toBe(12225730);
Copy link
Contributor

Choose a reason for hiding this comment

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

There is a bug in the algorithm. 1234 * 5678 = 7006652

});
});
32 changes: 32 additions & 0 deletions src/algorithms/math/bits/karatsubaMultiplication.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* @param {number} number
* @return bool
*/
export default function karatsubaMultiplication(A, B) {
const base = 10;

if ((A < base) || (B < base)) {
return A * B;
}

const tempA = A.toString();
const tempB = B.toString();

const num = (tempA.length > tempB.length) ? tempB.length : tempA.length;
const multiplier = Math.round(num >> 1);

const highBits1 = parseInt(tempA.substring(0, tempA.length - multiplier), base);
const lowBits1 = parseInt(tempA.substring(tempA.length - multiplier, tempA.length), base);

const highBits2 = parseInt(tempB.substring(0, tempB.length - multiplier), base);
const lowBits2 = parseInt(tempB.substring(tempB.length - multiplier, tempB.length), base);


const res0 = karatsubaMultiplication(lowBits1, lowBits2);
const res1 = karatsubaMultiplication(lowBits1 + highBits1, lowBits2 + highBits2);
const res2 = karatsubaMultiplication(highBits1, highBits2);

const res = (res2 ** (10, multiplier << 1)) + ((res1 - res2 - res0) ** (10, multiplier)) + res0;

return res;
}