-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib.js
47 lines (39 loc) · 851 Bytes
/
lib.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
function avg(numbers) {
let s = numbers.reduce((prev, curr) => prev + curr, 0);
return s / numbers.length;
}
function prime(num) {
if(num <= 1) {
return false;
}
if(num%1 !== 0){
console.log("not input positive integers")
process.exit(-1)
}
if( num % 2 === 0) {
return num === 2 ? true : false;
}
const sqrt = parseInt(Math.sqrt(num));
for(let divider = 3; divider <= sqrt; divider += 2) {
if(num % divider === 0) {
return false;
}
}
return true;
}
function fact(num) {
if(num >= 15){
console.log("please input less then 15")
process.exit(-1)
}
let result = num;
for(let i = num-1; i>0; i--){
result *= i;
}
return result;
}
module.exports = {
avg,
prime,
fact
}