|
1 | 1 | # Criss-Cross Multiplication
|
| 2 | + |
2 | 3 |
|
3 |
| -The criss-cross multiplication algorithm multiplies large positive integers in string format. This algorithm is nearly twice as fast as the Karatsuba algorithm when the number of digits is below 1000. |
| 4 | +A 50-lines algorithm for multiplying large positive integers in string format. For numbers having less than 100 digits, it outperforms the Karatsuba algorithm by as much as 50%. |
4 | 5 |
|
5 | 6 | <img src="3x3.gif" width="300" height="300">
|
6 | 7 |
|
7 | 8 | Read more about the algorithm in this [post](https://creme332.github.io/creamy-notes/posts/criss-cross-multiplication/).
|
8 | 9 |
|
| 10 | + |
| 11 | +## Installation |
| 12 | + |
| 13 | +To install project: |
| 14 | +``` |
| 15 | +git clone [email protected]:creme332/criss-cross-algorithm.git |
| 16 | +``` |
| 17 | + |
9 | 18 | ## Usage
|
| 19 | +Import `mul.h` in your program and initialise a `Mul` object as follows: |
| 20 | +```cpp |
| 21 | +Mul Multiplier("9912", "54564"); |
| 22 | +Multiplier.vedic(); // 540838368 |
| 23 | +``` |
| 24 | +
|
| 25 | +The `Mul` class has 3 methods for multiplication: |
| 26 | +
|
| 27 | +| Method | Description | |
| 28 | +| -------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------- | |
| 29 | +| `basicVedic()` | A simpler and faster implementation of the criss-cross algorithm but is more limited since it uses basic arithmetic operators for addition and subtraction. | |
| 30 | +| `vedic()` | A version of `basicVedic()` that uses helper functions for adding and subtracting large string numbers. | |
| 31 | +| `karatsuba()` | Uses the Karatsuba algorithm for multiplication. Algorithm uses helper functions for adding and subtracting large string numbers. | |
| 32 | +
|
| 33 | +## Run benchmarks |
| 34 | +To run benchmarks: |
| 35 | +``` |
| 36 | +g++ benchmarks/main.cpp benchmarks/timer.cpp src/mul.cpp -W |
| 37 | +./a.out |
| 38 | +``` |
| 39 | +A folder `output` will be created inside the `benchmarks` folder. This new folder will contain the following files: |
| 40 | +
|
| 41 | +| File name | Content | |
| 42 | +| ------------- | -------------------------------------------------------------------------------------------------------------------------------------------- | |
| 43 | +| `input.txt` | A list of multiplicands and multipliers which were generated during runtime. | |
| 44 | +| `time.txt` | Each line contains 2 values representing the time taken for vedic and karatsuba algorithms respectively | |
| 45 | +| `product.txt` | Each line contains 2 values representing the product calculated by vedic and karatsuba algorithms respectively. These values should be same. | |
| 46 | +
|
| 47 | +
|
| 48 | +## Run tests |
| 49 | +
|
| 50 | +```bash |
| 51 | +g++ test_runner.cpp tests/tests.cpp src/mul.cpp -W |
| 52 | +./a.out |
| 53 | +``` |
10 | 54 |
|
11 | 55 | ## To-do
|
12 | 56 |
|
13 |
| -* [ ] Use object oriented approach |
14 |
| -* [ ] Write tests |
15 |
| -* [ ] Add github workflow |
16 |
| -* [ ] add badges |
17 |
| -* [ ] Release |
18 |
| -* [ ] Store `sum` and `carry` as strings instead of long long data type. Then use addition and subtraction function for strings. |
| 57 | +* [ ] Use Google Benchmark for benchmarks |
19 | 58 |
|
20 | 59 | ## References
|
| 60 | + |
| 61 | +- [Criss cross algorithm](https://www.youtube.com/watch?v=JhGzbN5YuPo) |
| 62 | +- [Karatsuba algorithm](https://drawar.github.io/karatsuba-cpp/) |
| 63 | +- [Code for benchmarking](https://www.youtube.com/watch?v=YG4jexlSAjc&ab_channel=TheCherno) |
0 commit comments