forked from raycast/script-commands
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalculate-combinations.js
executable file
·71 lines (59 loc) · 1.59 KB
/
calculate-combinations.js
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#!/usr/bin/env node
// Dependency: This script requires Nodejs.
// Install Node: https://nodejs.org/en/download/
// Required parameters:
// @raycast.schemaVersion 1
// @raycast.title nCr: Calculate Combinations
// @raycast.mode silent
// Optional parameters:
// @raycast.icon 𝐂
// @raycast.argument1 { "type": "text", "placeholder": "n" }
// @raycast.argument2 { "type": "text", "placeholder": "r" }
// Documentation:
// @raycast.description nCr: Calculate combinations
// @raycast.author cSharp
// @raycast.authorURL https://github.com/noidwasavailable
const child_process = require('child_process');
// Function to copy data to clipboard
const pbcopy = (data) => {
return new Promise(function (resolve, reject) {
const child = child_process.spawn('pbcopy');
child.on('error', function (err) {
reject(err);
});
child.on('close', function (err) {
resolve(data);
});
child.stdin.write(data);
child.stdin.end();
});
};
// get args
const n = Number(process.argv.slice(2)[0]);
const r = Number(process.argv.slice(2)[1]);
// performance optimizations
const memoize = (fn) => {
const cache = {};
return (...args) => {
const n = args[0];
if (n in cache) {
return cache[n];
}
const result = fn(n);
cache[n] = result;
return result;
};
};
// factorial function
const factorial = memoize((n) => {
if (n === 0 || n === 1) {
return 1;
} else {
return n * factorial(n - 1);
}
});
// calculate nCr
const nCr = factorial(n) / (factorial(r) * factorial(n - r));
console.log(`${n} choose ${r} = ${nCr}`);
pbcopy(`${nCr}`);
return nCr;