RosettaCodeData/Task/Combinations-and-permutations/Zig/combinations-and-permutations.zig
2023-10-02 18:11:16 -07:00

32 lines
701 B
Zig

const std = @import("std");
const num = f64;
pub fn perm(n: num, k: num) num {
var result: num = 1;
var i: num = 0;
while (i < k) : (i += 1) {
result *= n - i;
}
return result;
}
pub fn comb(n: num, k: num) num {
return perm(n, k) / perm(k, k);
}
pub fn main() !void {
var stdout = std.io.getStdOut().writer();
const p: num = 12;
const c: num = 60;
var j: num = 1;
var k: num = 10;
while (j < p) : (j += 1) {
try stdout.print("P({d},{d}) = {d}\n", .{ p, j, @floor(perm(p, j)) });
}
while (k < c) : (k += 10) {
try stdout.print("C({d},{d}) = {d}\n", .{ c, k, @floor(comb(c, k)) });
}
}