Struggling a bit with BigInt

Hey, I'm struggling a bit with using the BigInt library, and feel I may misunderstand how I'm supposed to use it. I'd like to be able to use arbitrarily large powers, up to say, 100^100. But the code below, at 10^10, is pretty much where the buck stops. I think I may actually be running into overflow problems on checked_pow, but I'm not sure. In that case, how should I implement pow for BigInt such that this works?

fn main() {
    let m:Vec<BigInt> = iproduct!(2..10,2..10)
    .map(|(a,b)| BigInt::from(num::checked_pow(a,b)
    .expect("failed on")))
    .unique()
    .collect();
    println!("{:?}", m.len());
}

playground

First, note that 2..10 excludes the endpoint, so your example is only going up to 9^9.

You're only passing the default i32 type to checked_pow, so the maximum possible is 2^31-1. Even 10^10 will overflow this. But if you do your math with a BigInt base, you can go as high as you have time/memory to compute. So try num::pow(BigInt::from(a), b).

1 Like

Yep, does it. Thanks @cuviper!
Updated playground for anyone following along. https://play.rust-lang.org/?version=nightly&mode=debug&edition=2018&gist=2bdff5113961912d07390adf041487d6

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.