accepted bigint

This commit is contained in:
Joe Ardent 2022-10-28 17:23:37 -07:00
parent 8a60920998
commit e3de039c39
1 changed files with 14 additions and 8 deletions

View File

@ -1,7 +1,10 @@
pub fn multiply(num1: String, num2: String) -> String {
//
let mut out: Vec<u32> = Vec::from_iter((0..(num1.len() + num2.len())).into_iter().map(|_| 0));
let mut out: Vec<u32> = (0..(num1.len() + num2.len()))
.into_iter()
.map(|_| 0)
.collect();
let l1 = num1.len();
let l2 = num2.len();
@ -11,7 +14,7 @@ pub fn multiply(num1: String, num2: String) -> String {
for (i2, d2) in num2.chars().rev().enumerate() {
let mut mcarry = 0;
let mut tmp = Vec::from_iter((0..i2).into_iter().map(|_| 0));
let mut tmp: Vec<u32> = (0..i2).into_iter().map(|_| 0).collect();
let d2 = d2.to_digit(10).unwrap();
for d1 in num1.chars().rev() {
let d1 = d1.to_digit(10).unwrap();
@ -25,16 +28,17 @@ pub fn multiply(num1: String, num2: String) -> String {
}
merge(&mut out, &tmp);
}
let out = if let Some(last) = out.last() {
while let Some(last) = out.last() {
if *last == 0 {
let len = out.len() - 1;
&out[0..len]
out = out[0..len].to_vec();
} else {
&out[0..]
break;
}
} else {
&out[0..]
};
}
if out.is_empty() {
out.push(0);
}
out.iter().rev().map(|d| d.to_string()).collect()
}
@ -64,4 +68,6 @@ fn main() {
.to_string(),
"4000000000000000000000000000000000000000000000000000000000000000".to_string()
));
dbg!(multiply("0".to_string(), "8".to_string()));
}