最接近素数的数字-JavaScript
我们需要编写一个JavaScript函数,该函数接受一个数字并返回出现在n之后的第一个素数。
例如:如果数字是24,
然后输出应该是29
示例
以下是代码-
const num = 24;
const isPrime = n => {
if (n===1){
return false;
}else if(n === 2){
return true;
}else{
for(let x = 2; x < n; x++){
if(n % x === 0){
return false;
}
}
return true;
};
};
const nearestPrime = num => {
while(!isPrime(++num)){};
return num;
};
console.log(nearestPrime(24));输出结果
以下是控制台中的输出-
29
热门推荐
10 对同事简短生日祝福语
11 儿子31岁祝福语简短
12 婚礼祝福语简短情话文案
13 同学道别祝福语简短
14 讲课祝福语简短10字
15 哥哥结婚的祝福语简短
16 新年的生日祝福语简短
17 婚礼仪式祝福语简短
18 姨父生日祝福语简短精辟