Last active
August 15, 2024 16:16
-
-
Save casweeney/230e88b952eafc76c40d1ccb9624abd2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fn main() { | |
println!("{}", factorial(4)); | |
} | |
// Using Recursion | |
// fn factorial(n: usize) -> usize { | |
// if( n == 0) { | |
// return 1; | |
// } else { | |
// return factorial(n-1) * n; | |
// } | |
// } | |
// Using Loop | |
fn factorial(n: usize) -> usize { | |
let mut result: usize = 1; | |
let mut i = 1; | |
while i <= n { | |
result *= i; | |
i+=1; | |
}; | |
result | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment