Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Semrush - keyword research tool

Create your online store today with Shopify

Turn ChatGPT, Claude, Gemini, And CoPilot Into Your Personal Assistant, Business Coach, Content Creator, And More

AFFILIATE MARKETING Your all-in-one performance engine Manage affiliates, creators, and customer referrals in one unified platform—turning every partnership into measurable growth

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Disclosure: My content contains affiliate links.

43,181 questions

56,073 answers

573 users

How to compute the factorial of a number greater than 20 in Rust

1 Answer

0 votes
use num_bigint::BigUint;
use num_traits::{One};
use std::io;

/*
    This program computes the factorial of numbers greater than 20.
    Rust does not include arbitrary‑precision integers in the standard library,
    but the num_bigint crate provides BigUint, which grows as needed.
*/

/*
    Compute factorial using BigUint.
    The algorithm multiplies numbers from 2 to n.
    BigUint handles overflow internally and expands automatically.
*/
fn factorial_big(n: u64) -> BigUint {
    let mut result: BigUint = One::one();

    for i in 2..=n {
        result *= i;
    }

    result
}

/*
    Main entry point: read input, compute factorial, print result.
*/
fn main() {
    println!("Enter a number greater than 20:");

    let mut input = String::new();
    io::stdin().read_line(&mut input).expect("Failed to read input");

    let n: u64 = input.trim().parse().expect("Please enter a valid integer");

    let result = factorial_big(n);

    println!("\nFactorial of {} is:\n", n);
    println!("{}", result);
}



/*
run:

Enter a number greater than 20: 25

Factorial of 25 is:

15511210043330985984000000

*/

 



answered 3 days ago by avibootz

Related questions

...