Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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.

License

danishayman/Simple-Multiplication-Algorithm

Repository files navigation

๐Ÿงฎ Simple Multiplication Algorithm vs Karatsuba Algorithm

๐Ÿ“‹ Table of Contents

๐ŸŒŸ Overview

This project implements and compares two multiplication algorithms:

  1. Simple Multiplication Algorithm - A traditional approach with O(n2) 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(n2)
    • Nested loops for digit-by-digit multiplication
    • Most significant part of the algorithm
  4. Handling Carries and Partial Products โž•

    • Time Complexity: O(n2)
    • 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(n2) โฑ๏ธ

๐Ÿš€ 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 * 10n + ((a+b)(c+d) - ac - bd) * 10n/2 + 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:

    javac SimpleMultiplication.java
    javac Karatsuba.java
  2. Run the algorithms:

    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(n2) 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.

About

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.

Topics

Resources

License

Stars

Watchers

Forks

Contributors 2

Languages

AltStyle ใซใ‚ˆใฃใฆๅค‰ๆ›ใ•ใ‚ŒใŸใƒšใƒผใ‚ธ (->ใ‚ชใƒชใ‚ธใƒŠใƒซ) /