求最大公约数
const gcd = function (a, b) {
if (a % b == 0) return b;
return gcd(b, a % b);
}
判断字符串是否为回文串
const isPalindrome = function (s) {
return s === s.split('').reverse().join('');
}
判断数字是否为质数
const isPrime = function (n) {
for (let i = 2; i < Math.sqrt(n) >> 0; i++) {
if (n % i === 0) return false;
}
return true;
}
判断数组中是否存在相同元素
arr.length === new Set(arr).size
求阶乘
const factorial = function (num) {
if (num === 0) return 1;
return num * factorial(num - 1);
}
二进制中1的个数
const number2 = function (num) {
let sum = 0;
while (num) {
num = num & (num - 1);
sum++;
}
return sum;
}