This project implements and compares two multiplication algorithms:
- Simple Multiplication Algorithm - A traditional approach with O(n2) complexity
- Karatsuba Algorithm - A faster divide-and-conquer approach
- Java Development Kit (JDK) 8 or higher
- Basic understanding of algorithm complexity
- Familiarity with Java programming
The simple multiplication algorithm is implemented with the following steps and their time complexities:
-
Generating Random Numbers ๐ฒ
- Time Complexity: O(n)
- Generates random numbers with n digits
- Constant time operation for each digit
-
Storing Digits in Arrays ๐ฆ
- Time Complexity: O(n)
- Iterates once over n digits
- Stores each digit in separate arrays
-
Performing Multiplication โ๏ธ
- Time Complexity: O(n2)
- Nested loops for digit-by-digit multiplication
- Most significant part of the algorithm
-
Handling Carries and Partial Products โ
- Time Complexity: O(n2)
- Manages carries and accumulates partial products
- Nested within multiplication loops
-
Result Output ๐
- Time Complexity: O(n)
- Linear iteration over result digits
- Formats and displays the final product
Overall Time Complexity: O(n2) โฑ๏ธ
A faster multiplication algorithm that reduces the number of multiplication operations through recursive decomposition.
-
Base Case โก
- If n = 1, uses simple multiplication
- Direct multiplication for single-digit numbers
-
Recursive Decomposition ๐
- Splits numbers into halves (a, b) and (c, d)
- Performs three recursive multiplications:
- ac
- bd
- (a+b)(c+d)
-
Final Computation ๐ฏ
- Combines results using the formula:
- ac * 10n + ((a+b)(c+d) - ac - bd) * 10n/2 + bd
- Reduces multiplication operations
- More efficient for large numbers
- Recursive approach with three subproblems
- Both algorithms are implemented in Java
- Includes performance counters for operation tracking
- Supports random number generation for testing
- Validates results through assertions
.
โโโ SimpleMultiplication.java # Simple multiplication implementation
โโโ Karatsuba.java # Karatsuba algorithm implementation
โโโ Graph of Karatsuba Algorithm.xlsx # Performance graphs
โโโ Graph for Simple Multiplication.xlsx # Performance graphs
-
Compile the Java files:
javac SimpleMultiplication.java javac Karatsuba.java
-
Run the algorithms:
java SimpleMultiplication java Karatsuba
-
Configure parameters:
- Modify
numberOfDigitsin both files to change input size - Adjust
MAX_VALUEto change number of test iterations
- Modify
| Algorithm | Time Complexity | Best For | Space Complexity |
|---|---|---|---|
| Simple | O(n2) | Small numbers | O(n) |
| Karatsuba | O(n^1.585) | Large numbers | O(n) |
- 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
The project includes Excel files with performance graphs:
Graph of Karatsuba Algorithm.xlsxGraph for Simple Multiplication.xlsx
These graphs visualize:
- Operation count vs. input size
- Time complexity comparison
- Performance differences between algorithms
Feel free to:
- Report issues
- Suggest improvements
- Submit pull requests
This project is open source and available for educational purposes.