Skip to content

Factorial calculated extremely slow #234

@mrLSD

Description

@mrLSD
extern crate num;

use num::{BigInt, BigUint, Zero, One, FromPrimitive};

fn factorial(n: usize) -> BigUint {
    let mut f: BigUint = One::one();
    for i in 1..(n+1) {
        let bu: BigUint = FromPrimitive::from_usize(i).unwrap();
        f = f * bu;
    }
    f
}

fn main() {
    println!("{}", factorial(1_000_000));
}

Execute:

$ cargo build --release
$ tiem ./factorial
real    20m46.867s
user    18m15.180s
sys 2m28.616s

For example:
gcc: 2m.20s

What is wrong?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions