在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 新房开工吉日祝福语简短
12 50多岁生日简短祝福语
13 安徽疫情祝福语简短英语
14 农民朋友发财祝福语简短
15 对生活祝福语简短精辟
16 搬家词简短祝福语朋友
17 女神结婚快乐祝福语简短
18 文学短句祝福语大全简短