我需要在我的项目中计算21阶乘。
fn factorial(num: u64) -> u64 {
match num {
0 => 1,
1 => 1,
_ => factorial(num - 1) * num,
}
}
fn main() {
let x = factorial(21);
println!("The value of 21 factorial is {} ", x);
}
运行此代码时,出现错误:
thread 'main' panicked at 'attempt to multiply with overflow', src\main.rs:5:18
最佳答案
u64
不能容纳21个! (介于2 ^ 65和2 ^ 66之间),但是u128
可以。
关于rust - 如何计算Rust中的21阶乘?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59206653/