Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Factorial exercise #15

Closed
carloslbello opened this issue Sep 24, 2015 · 1 comment
Closed

Factorial exercise #15

carloslbello opened this issue Sep 24, 2015 · 1 comment

Comments

@carloslbello
Copy link

I think a factorial exercise would help people learn about Ranges and .fold(). As an example solution:

fn factorial(num: u64) -> u64 {
    (1..num + 1).fold(1, |prod, x| prod * x)
}
@carols10cents
Copy link
Member

Closed by #56!! 🎉

cedrickchee pushed a commit to cedrickchee/rustlings-solutions that referenced this issue Nov 15, 2019
Tackles rust-lang/rustlings#15 . I think this allows for varying levels of difficulty depending on a users background. But ideally people can move there way up from 1, 2, 3 (Numbers below).

1. 
    let mut result = 1;
    for i in 1..x + 1 {
        result *= i;
    }
    result

2. 
    match x {
        0 | 1 => 1,
        x => x * factorial(x - 1),
    }
3. 
    (1..x + 1).product()
ppp3 pushed a commit to ppp3/rustlings that referenced this issue May 23, 2022
Tackles rust-lang#15 . I think this allows for varying levels of difficulty depending on a users background. But ideally people can move there way up from 1, 2, 3 (Numbers below).

1. 
    let mut result = 1;
    for i in 1..x + 1 {
        result *= i;
    }
    result

2. 
    match x {
        0 | 1 => 1,
        x => x * factorial(x - 1),
    }
3. 
    (1..x + 1).product()
FancyGeekGuru added a commit to FancyGeekGuru/rust-lings that referenced this issue May 24, 2022
Tackles rust-lang/rustlings#15 . I think this allows for varying levels of difficulty depending on a users background. But ideally people can move there way up from 1, 2, 3 (Numbers below).

1. 
    let mut result = 1;
    for i in 1..x + 1 {
        result *= i;
    }
    result

2. 
    match x {
        0 | 1 => 1,
        x => x * factorial(x - 1),
    }
3. 
    (1..x + 1).product()
beephsupreme added a commit to beephsupreme/rustlings that referenced this issue Jul 25, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants