Combinations and permutations: Difference between revisions

Moved Rust to the proper place
(Moved Rust to the proper place)
Line 2,505:
Ruby's Arrays have a permutation and a combination method which result in (lazy) enumerators. These Enumerators have a "size" method, which returns the size of the enumerator, or nil if it can’t be calculated lazily. (Since Ruby 2.0)
<syntaxhighlight lang="ruby">(1..60).to_a.combination(53).size #=> 386206920</syntaxhighlight>
 
=={{header|Rust}}==
 
{{trans|zig}}
 
Almost a verbatim port... the logic is retained but I renounce anyway to use `io::stdout()`, `println!()` is enough. I wonder why f64?!
 
<syntaxhighlight lang="rust">
 
fn perm(n: f64, k: f64) -> f64 {
let mut result: f64 = 1.0;
let mut i: f64 = 0.0;
 
while i < k {
result *= n - i;
i += 1.0;
}
 
result
}
 
fn comb(n: f64, k: f64) -> f64 {
perm(n, k) / perm(k, k)
}
 
fn main() {
const P: f64 = 12.0;
const C: f64 = 60.0;
 
let mut j: f64 = 1.0;
let mut k: f64 = 10.0;
 
while j < P {
println!("P({},{}) = {}", P, j, perm(P, j).floor());
j += 1.0;
}
 
while k < C {
println!("C({},{}) = {}", C, k, comb(C, k).floor());
k += 10.0;
}
 
}
</syntaxhighlight>
 
{{out}}
<pre>
P(12,1) = 12
P(12,2) = 132
P(12,3) = 1320
P(12,4) = 11880
P(12,5) = 95040
P(12,6) = 665280
P(12,7) = 3991680
P(12,8) = 19958400
P(12,9) = 79833600
P(12,10) = 239500800
P(12,11) = 479001600
C(60,10) = 75394027566
C(60,20) = 4191844505805495
C(60,30) = 118264581564861420
C(60,40) = 4191844505805495
C(60,50) = 75394027566
</pre>
 
=={{header|Scheme}}==
Line 3,187 ⟶ 3,251:
}
}</syntaxhighlight>
 
{{out}}
<pre>
P(12,1) = 12
P(12,2) = 132
P(12,3) = 1320
P(12,4) = 11880
P(12,5) = 95040
P(12,6) = 665280
P(12,7) = 3991680
P(12,8) = 19958400
P(12,9) = 79833600
P(12,10) = 239500800
P(12,11) = 479001600
C(60,10) = 75394027566
C(60,20) = 4191844505805495
C(60,30) = 118264581564861420
C(60,40) = 4191844505805495
C(60,50) = 75394027566
</pre>
 
 
=={{header|Rust}}==
 
{{trans|zig}}
 
Almost a verbatim port... the logic is retained but I renounce anyway to use `io::stdout()`, `println!()` is enough. I wonder why f64?!
 
<syntaxhighlight lang="rust">
 
fn perm(n: f64, k: f64) -> f64 {
let mut result: f64 = 1.0;
let mut i: f64 = 0.0;
 
while i < k {
result *= n - i;
i += 1.0;
}
 
result
}
 
fn comb(n: f64, k: f64) -> f64 {
perm(n, k) / perm(k, k)
}
 
fn main() {
const P: f64 = 12.0;
const C: f64 = 60.0;
 
let mut j: f64 = 1.0;
let mut k: f64 = 10.0;
 
while j < P {
println!("P({},{}) = {}", P, j, perm(P, j).floor());
j += 1.0;
}
 
while k < C {
println!("C({},{}) = {}", C, k, comb(C, k).floor());
k += 10.0;
}
 
}
</syntaxhighlight>
 
{{out}}
3,038

edits