2022-10-18 12:35:44 +00:00
|
|
|
fn fact_recursive(n) {
|
2022-09-12 20:53:04 +00:00
|
|
|
if n <= 1 {
|
|
|
|
return 1;
|
|
|
|
}
|
2022-10-18 12:35:44 +00:00
|
|
|
return n * fact_recursive(n-1);
|
2022-09-12 20:53:04 +00:00
|
|
|
}
|
|
|
|
|
2022-10-18 12:35:44 +00:00
|
|
|
fn fact_imperative(n) {
|
|
|
|
if n <= 1 {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
let res = 1;
|
|
|
|
for i: 1..=n {
|
|
|
|
res *= i;
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
fn fact_pipeline(n) {
|
|
|
|
if n <= 1 {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 1..=n |// \*;
|
2022-09-12 20:53:04 +00:00
|
|
|
}
|
2022-10-18 12:35:44 +00:00
|
|
|
|
|
|
|
# 10*9*8*7*6*5*4*3*2*1 = 10
|
|
|
|
println(fact_recursive(10));
|
|
|
|
println(fact_imperative(10));
|
|
|
|
println(fact_pipeline(10));
|