如何计算21锈因数

问题描述 投票:0回答:1

我需要在我的项目中计算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

rust factorial cargo
1个回答
0
投票

21!不适合64位int。您需要一些arbitrary precision arithmetic(或bigint)库。

© www.soinside.com 2019 - 2024. All rights reserved.