Skip to content

Commit 6c56ad1

Browse files
Corrigindo fatorial recursivo
1 parent 8ff18f5 commit 6c56ad1

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

src/rust/fatorial_recursiva.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,23 @@
1111

1212
// Para realizar uma fatoração com recursão basta fazer o retorno
1313
// de uma função ser valor * a propia função recebendo valor - 1
14-
fn fatorial(valor: usize) -> usize {
14+
fn fatorial(valor: u128) -> u128 {
1515
// Para que não chege a multiplicar por 0 quando chegamos a 1 ou 0
1616
// é retornado 1 para que o utlimo valor sejá multilpicado por 1
17-
if valor <= 1 {
18-
return 1;
17+
match valor {
18+
0 | 1 => 1,
19+
2.. => valor * (fatorial(valor - 1)),
1920
}
20-
return valor * (fatorial(valor - 1));
2121
}
2222

2323
fn main() {
24-
println!("{}", fatorial(10));
24+
println!("{}", fatorial(21));
2525
}
2626

2727
#[cfg(test)]
2828
mod test {
2929
use super::*;
30+
#[test]
3031
fn teste_fatorial() {
3132
assert_eq!(fatorial(0), 1);
3233
assert_eq!(fatorial(1), 1);

0 commit comments

Comments
 (0)