Is calculating BigInt.pow(&BigInt) possible?

54 Views Asked by At

I want to calculate:

let n1 = BigInt(256);
let n2 = BigInt(1024);
let n3 = BigInt(3);
let n4 = BigInt(40);

let res = n1.pow(n2.pow(n3) * n4);
let len = res.to_string().len();

I tried num crate, but its pow() method only supports u32 as exponent. Is there any crate that supports BigInt as exponent? Or is it just too big to be calculated?

1

There are 1 best solutions below

0
Lomírus On

Not an answer for the BigInt.pow(&BigInt), but only an answer to this specific problem. This question in this post is "How many digits does this number have?" and it can be converted to a math problem.

First simplify this expression.

BigInt(256).pow(BigInt(1024).pow(BigInt(3)) * BigInt(40))
=> 256 ^ (1024 ^ 3 * 40)
=> 2 ^ (2^36 * 5)

And "how many digits" can be calculated by "log(10, X)" for decimal number.

log(10, 2 ^ (2^36 * 5))
= 2 ^ 36 * 5 * log(10, 2)

Which can be calculated easily. And the result is approximately 103433118919.34569.