Skip to content

Instantly share code, notes, and snippets.

@casweeney
Last active August 15, 2024 16:16
Show Gist options
  • Save casweeney/230e88b952eafc76c40d1ccb9624abd2 to your computer and use it in GitHub Desktop.
Save casweeney/230e88b952eafc76c40d1ccb9624abd2 to your computer and use it in GitHub Desktop.
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