-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscripts.js
More file actions
43 lines (41 loc) · 1.13 KB
/
scripts.js
File metadata and controls
43 lines (41 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
function factiorial(n) {
let ans = 1n; // big int
for(let i = 1n; i <= n; i++) {
ans *= i;
}
return ans;
}
function permutations(n, r) {
return (factiorial(n) / factiorial(n-r) );
}
function combinations(n, r) {
return (factiorial(n) / factiorial(n-r) / factiorial(r) );
}
function permutationsRepetition(n, list) {
let ans = factiorial(n);
for(let i=0; i<list.length; i++) {
ans /= factiorial(list[i]);
}
return ans;
}
// toLocaleString to add commas
$( ()=> {
$('#perm-calc').click( ()=> {
let n = parseInt($('#perm-n').val() );
let r = parseInt($('#perm-r').val() );
let ans = permutations(n, r);
$('#perm-output').html( ans.toLocaleString() );
});
$('#comb-calc').click( ()=> {
let n = parseInt($('#comb-n').val() );
let r = parseInt($('#comb-r').val() );
let ans = combinations(n, r);
$('#comb-output').html( ans.toLocaleString() );
});
$('#perm-rept-calc').click( ()=> {
let n = parseInt($('#perm-rept-n').val() );
let list = ($('#perm-rept-list').val().trim().split(',') ).map( x => parseInt(x) );
let ans = permutationsRepetition(n, list);
$('#perm-rept-output').html( ans.toLocaleString() );
});
});