I solved Project Euler #4 in Rust. There is one line of code that took me around 30 minutes to solve. When I remove the line, I get: thread 'main' panicked at 'attempt to multiply with overflow' Explanation is in the code:(see on playground)
The weird thing is, rev is 0, but when I try: rev=0; in the place that I have marked as 'problem here', it will solve the issue even though the value is same. Why is that? I have checked and this is not a duplicate question. I also didn't know what to write in the title since this is an uncommon error.
//Task: Find the largest palindrome made from the product of two 3-digit numbers.
fn main(){
let mut pal;//palindrome
let mut ram;//a second number that's equal to palindrome, to copy it's digits.
let mut rev=0;//reversed palindrome
let mut lar=0;//largest palindrome
for ln in 100..1000{//ln=left number } left number * right number = palindrome
for rn in 100..1000{//rn=right number} ^
pal=ln*rn;// |
ram=pal;
//-----------------Problem here-----------------
//rev=ram%10;//when this line is commented, it gives:
//thread 'main' panicked at 'attempt to multiply with overflow', why_overflow_pe4.rs:13:17
while ram>0{//getting the last digit of ram for the first digit of rev
//and continuing until ram=0 and rev is reversed.
rev*=10;
ram/=10;
rev+=ram%10;
}
rev/=10;
if pal==rev && pal>lar{//if rev=pal and our palindrome is larger than previous largest
//make the largest palindrome current palindrome
lar=pal;
}
}
}
println!("{}",lar);
}
-Thank you!