-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPermutationsAndCombinations.c
More file actions
51 lines (43 loc) · 1.13 KB
/
PermutationsAndCombinations.c
File metadata and controls
51 lines (43 loc) · 1.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <stdio.h>
#include <time.h>
double factorial_with_ll(int number, int ll) // ll - lower limit
{
if (number < 2) return 1;
double product = 1;
for (double i = number; i > ll; --i)
{
product *= i;
}
return product;
}
double combinations(int n, int r)
{
if (n - r < r) r = n - r;
return factorial_with_ll(n, n - r) / factorial_with_ll(r, 1);
}
double permutations(int n, int r)
{
return factorial_with_ll(n, n - r);
}
double duration(clock_t start, clock_t end)
{
return (double)(end - start)/CLOCKS_PER_SEC;
}
void main()
{
int n, r;
scanf("%d %d", &n, &r);
clock_t start1, end1, start2, end2;
double duration_perm, duration_comb;
start1 = clock();
double noof_perms = permutations(n, r);
end1 = clock();
duration_perm = duration(start1, end1);
start2 = clock();
double noof_combs = combinations(n, r);
end2 = clock();
duration_comb = duration(start2, end2);
printf("(Perms, Combs) = (%lf, %lf)\n", noof_perms, noof_combs);
printf("\nFor perms, start = %ld, end = %ld, duration = %lf", start1, end1, duration_perm);
printf("\nFor combs, start = %ld, end = %ld, duration = %lf\n", start2, end2, duration_comb);
}