Skip to content

feat: add multiply.rs #716

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

Merged
merged 11 commits into from
May 23, 2024
Merged
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
1 change: 1 addition & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
* [Sudoku](https://github.com/TheAlgorithms/Rust/blob/master/src/backtracking/sudoku.rs)
* Big Integer
* [Fast Factorial](https://github.com/TheAlgorithms/Rust/blob/master/src/big_integer/fast_factorial.rs)
* [Multiply](https://github.com/TheAlgorithms/Rust/blob/master/src/big_integer/multiply.rs)
* [Poly1305](https://github.com/TheAlgorithms/Rust/blob/master/src/big_integer/poly1305.rs)
* Bit Manipulation
* [Counting Bits](https://github.com/TheAlgorithms/Rust/blob/master/src/bit_manipulation/counting_bits.rs)
Expand Down
2 changes: 2 additions & 0 deletions src/big_integer/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#![cfg(feature = "big-math")]

mod fast_factorial;
mod multiply;
mod poly1305;

pub use self::fast_factorial::fast_factorial;
pub use self::multiply::multiply;
pub use self::poly1305::Poly1305;
77 changes: 77 additions & 0 deletions src/big_integer/multiply.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/// Performs long multiplication on string representations of non-negative numbers.
pub fn multiply(num1: &str, num2: &str) -> String {
if !is_valid_nonnegative(num1) || !is_valid_nonnegative(num2) {
panic!("String does not conform to specification")
}

if num1 == "0" || num2 == "0" {
return "0".to_string();
}
let output_size = num1.len() + num2.len();

let mut mult = vec![0; output_size];
for (i, c1) in num1.chars().rev().enumerate() {
for (j, c2) in num2.chars().rev().enumerate() {
let mul = c1.to_digit(10).unwrap() * c2.to_digit(10).unwrap();
// It could be a two-digit number here.
mult[i + j + 1] += (mult[i + j] + mul) / 10;
// Handling rounding. Here's a single digit.
mult[i + j] = (mult[i + j] + mul) % 10;
}
}
if mult[output_size - 1] == 0 {
mult.pop();
}
mult.iter().rev().map(|&n| n.to_string()).collect()
}

pub fn is_valid_nonnegative(num: &str) -> bool {
num.chars().all(char::is_numeric) && !num.is_empty() && (!num.starts_with('0') || num == "0")
}

#[cfg(test)]
mod tests {
use super::*;
macro_rules! test_multiply {
($($name:ident: $inputs:expr,)*) => {
$(
#[test]
fn $name() {
let (s, t, expected) = $inputs;
assert_eq!(multiply(s, t), expected);
assert_eq!(multiply(t, s), expected);
}
)*
}
}

test_multiply! {
multiply0: ("2", "3", "6"),
multiply1: ("123", "456", "56088"),
multiply_zero: ("0", "222", "0"),
other_1: ("99", "99", "9801"),
other_2: ("999", "99", "98901"),
other_3: ("9999", "99", "989901"),
other_4: ("192939", "9499596", "1832842552644"),
}

macro_rules! test_multiply_with_wrong_input {
($($name:ident: $inputs:expr,)*) => {
$(
#[test]
#[should_panic]
fn $name() {
let (s, t) = $inputs;
multiply(s, t);
}
)*
}
}
test_multiply_with_wrong_input! {
empty_input: ("", "121"),
leading_zero: ("01", "3"),
wrong_characters: ("2", "12d4"),
wrong_input_and_zero_1: ("0", "x"),
wrong_input_and_zero_2: ("y", "0"),
}
}