1

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!

Cheng Chen
  • 42,509
  • 16
  • 113
  • 174
cubgnu
  • 13
  • 1
  • 3
  • Note that `rev` is zero on your problem line _only for the first iteration._ Ie. only when `ln == 100` and `rn == 100`. Afterwards `rev` keeps whatever value it had at the end of the previous iteration. – Jmb Aug 29 '20 at 15:09

1 Answers1

4

Inspecting the contents before the multiplication of rev may help: rev has the value 1010102010, and multiplying it by 10 would result in a number that is too large (i.e. would require too many bits) to be held by rev.

If you uncomment the rev=ram%10, rev will surely be less than 10, so multiplying it by 10 results in at most 100.

You can specify the data type size using u8, u16, u32, u64, u128 et al., but even u128 will overflow. Thus, you could either adapt your algorithm or go with a data type supporting more than 128 bits.

phimuemue
  • 34,669
  • 9
  • 84
  • 115
  • I didn't understand this part: "If you uncomment the rev=ram%10, rev will surely be less than 10, so multiplying it by 10 results in at most 100.", isn't rev less than 10?, when is it stops being 0 and becomes a large number? I mean it's value is already at 0, then I should get the same error when I write rev=0; in the place of comment. Why is it not happening? – cubgnu Aug 29 '20 at 14:54
  • `ram%10` computes `ram` modulo 10, which is the remainder of `ram` divided by 10. The remainder of a division must be smaller than the divisor (which is 10 in this case). – phimuemue Aug 29 '20 at 14:57
  • Yeah but, the thing I mean is here, run it in your machine. The browser is not showing anything to me: https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=e4c51793c7e20598e0b5b9a75e802cee – cubgnu Aug 29 '20 at 15:01
  • I think your assumption that `rev` is 0 is wrong: https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=ae2cb76815e951a81f0b523d19ea4dac. (It is not reset to 0 in the loop, only at the beginning.) – phimuemue Aug 29 '20 at 15:03
  • 1
    I've been in similar situations, and the `dbg!` macro helped me tremendously. – phimuemue Aug 29 '20 at 15:23