https://github.com/danishayman/simple-multiplication-algorithm
The Karatsuba algorithm is a fast multiplication algorithm that reduces the number of multiplication operations required to multiply two numbers by recursively breaking down the multiplication into smaller multiplications.
https://github.com/danishayman/simple-multiplication-algorithm
java karatsuba karatsuba-algorithm karatsuba-multiplication multiplication multiplication-algorithm
Last synced: 10 months ago
JSON representation
The Karatsuba algorithm is a fast multiplication algorithm that reduces the number of multiplication operations required to multiply two numbers by recursively breaking down the multiplication into smaller multiplications.
- Host: GitHub
- URL: https://github.com/danishayman/simple-multiplication-algorithm
- Owner: danishayman
- License: mit
- Created: 2024-04-16T15:43:18.000Z (almost 2 years ago)
- Default Branch: main
- Last Pushed: 2025-04-08T14:22:17.000Z (about 1 year ago)
- Last Synced: 2025-05-18T09:11:31.637Z (11 months ago)
- Topics: java, karatsuba, karatsuba-algorithm, karatsuba-multiplication, multiplication, multiplication-algorithm
- Language: Java
- Homepage:
- Size: 304 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# š§® Simple Multiplication Algorithm vs Karatsuba Algorithm
## š Table of Contents
- [Overview](#-overview)
- [Prerequisites](#-prerequisites)
- [Performance Analysis](#-performance-analysis)
- [Implementation Details](#-implementation-details)
- [Usage](#-usage)
- [Testing](#-testing)
- [Visual Comparison](#-visual-comparison)
## š Overview
This project implements and compares two multiplication algorithms:
1. Simple Multiplication Algorithm - A traditional approach with O(n²) complexity
2. Karatsuba Algorithm - A faster divide-and-conquer approach
## š§ Prerequisites
- Java Development Kit (JDK) 8 or higher
- Basic understanding of algorithm complexity
- Familiarity with Java programming
## š Performance Analysis
### š¢ Simple Multiplication Algorithm
The simple multiplication algorithm is implemented with the following steps and their time complexities:
1. **Generating Random Numbers** š²
- Time Complexity: O(n)
- Generates random numbers with n digits
- Constant time operation for each digit
2. **Storing Digits in Arrays** š¦
- Time Complexity: O(n)
- Iterates once over n digits
- Stores each digit in separate arrays
3. **Performing Multiplication** āļø
- Time Complexity: O(n²)
- Nested loops for digit-by-digit multiplication
- Most significant part of the algorithm
4. **Handling Carries and Partial Products** ā
- Time Complexity: O(n²)
- Manages carries and accumulates partial products
- Nested within multiplication loops
5. **Result Output** š
- Time Complexity: O(n)
- Linear iteration over result digits
- Formats and displays the final product
**Overall Time Complexity**: O(n²) ā±ļø
### š Karatsuba Algorithm
A faster multiplication algorithm that reduces the number of multiplication operations through recursive decomposition.
#### š How It Works:
1. **Base Case** ā”
- If n = 1, uses simple multiplication
- Direct multiplication for single-digit numbers
2. **Recursive Decomposition** š
- Splits numbers into halves (a, b) and (c, d)
- Performs three recursive multiplications:
- ac
- bd
- (a+b)(c+d)
3. **Final Computation** šÆ
- Combines results using the formula:
- ac * 10āæ + ((a+b)(c+d) - ac - bd) * 10āæ/² + bd
#### š Performance Benefits
- Reduces multiplication operations
- More efficient for large numbers
- Recursive approach with three subproblems
## š ļø Implementation Details
- Both algorithms are implemented in Java
- Includes performance counters for operation tracking
- Supports random number generation for testing
- Validates results through assertions
### š Project Structure
```
.
āāā SimpleMultiplication.java # Simple multiplication implementation
āāā Karatsuba.java # Karatsuba algorithm implementation
āāā Graph of Karatsuba Algorithm.xlsx # Performance graphs
āāā Graph for Simple Multiplication.xlsx # Performance graphs
```
## š» Usage
1. **Compile the Java files**:
```bash
javac SimpleMultiplication.java
javac Karatsuba.java
```
2. **Run the algorithms**:
```bash
java SimpleMultiplication
java Karatsuba
```
3. **Configure parameters**:
- Modify `numberOfDigits` in both files to change input size
- Adjust `MAX_VALUE` to change number of test iterations
## š Performance Comparison
| Algorithm | Time Complexity | Best For | Space Complexity |
|-----------|----------------|----------|------------------|
| Simple | O(n²) | Small numbers | O(n) |
| Karatsuba | O(n^1.585) | Large numbers | O(n) |
## š Testing
- Both algorithms are tested with random numbers
- Results are compared with expected values
- Includes assertion checks for accuracy
- Supports configurable number of digits and test iterations
## š Visual Comparison
The project includes Excel files with performance graphs:
- `Graph of Karatsuba Algorithm.xlsx`
- `Graph for Simple Multiplication.xlsx`
These graphs visualize:
- Operation count vs. input size
- Time complexity comparison
- Performance differences between algorithms
## š¤ Contributing
Feel free to:
- Report issues
- Suggest improvements
- Submit pull requests
## š License
This project is open source and available for educational purposes.