How to calculate value of nCr in Rust

2 Answers

0 votes
// Combinations Calculator - finds the number of possible combinations that can be taken from a set

fn factorial(n:i32)->i32 {
    if n == 1 || n == 0 {
        return 1;
    }
    
    return n * factorial(n - 1);
}

fn main() {
    let n:i32 = 7;
    let r:i32 = 3;

    let ncr:i32 = factorial(n) / (factorial(r) * factorial(n - r));

    println!("nCr = {}", ncr);
}




/*
run:
  
nCr = 35
  
*/

 



answered May 11, 2023 by avibootz
edited May 11, 2023 by avibootz
0 votes
// Combinations Calculator - finds the number of possible combinations that can be taken from a set

fn factorial(n:i32)->i32 {
    return if n == 1 || n == 0 { 1 } else { return n * factorial(n - 1) };
}

fn main() {
    let n:i32 = 7;
    let r:i32 = 3;

    let ncr:i32 = factorial(n) / (factorial(r) * factorial(n - r));

    println!("nCr = {}", ncr);
}




/*
run:
  
nCr = 35
  
*/

 



answered May 11, 2023 by avibootz
edited May 11, 2023 by avibootz
...