1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
use std::str::FromStr;
use num_bigint::{BigInt};
pub fn multiply(x: BigInt, y: BigInt) -> BigInt {
let x_len = x.to_string().len() as u32;
let y_len = y.to_string().len() as u32;
if x_len == 1 && y_len == 1 {
return x * y;
}
let half_x_len = x_len / 2;
let half_y_len = y_len / 2;
let x_multiplier = 10_u128.pow(half_x_len as u32);
let y_multiplier = 10_u128.pow(half_y_len as u32);
let b = x.clone() % (x_multiplier);
let d = y.clone() % (y_multiplier);
let a = x.clone() / (x_multiplier);
let c = y.clone() / (y_multiplier);
let ac = multiply(a.clone(), c.clone());
let bd = multiply(b.clone(), d.clone());
let ad = multiply(a.clone(), d.clone());
let bc = multiply(b.clone(), c.clone());
return 10_u128.pow((half_x_len + half_y_len) as u32) * ac + 10_u128.pow((half_x_len) as u32) * ad + 10_u128.pow((half_y_len) as u32) * bc + bd;
}
pub fn multiply_string(x_str:&str, y_str:&str) -> BigInt {
let x=BigInt::from_str(x_str).unwrap();
let y=BigInt::from_str(y_str).unwrap();
return multiply(x,y);
}
#[cfg(test)]
mod tests {
use crate::{multiply, multiply_string};
use num_bigint::BigInt;
use std::str::FromStr;
#[test]
fn multiply_works() {
let a = BigInt::from_str("1234567891011333456756333333333333335555555").unwrap();
let b = BigInt::from_str("12345678910113333453456").unwrap();
assert_eq!(BigInt::from(a.clone() * b.clone()).to_string(), multiply(a, b).to_string());
}
#[test]
fn multiply_string_works() {
let a_str = "1234567891011333456756333333333333335555555";
let b_str = "12345678910113333453456";
let a=BigInt::from_str(a_str).unwrap();
let b=BigInt::from_str(b_str).unwrap();
assert_eq!(BigInt::from(a.clone() * b.clone()).to_string(), multiply_string(&a_str, &b_str).to_string());
}
}