范围之间的质数之和-JavaScript
我们需要编写一个JavaScript函数,该函数接受两个数字,例如a和b,并返回介于a和b之间的所有素数的和。如果它们也是素数,我们应该包括a和b。
示例
以下是代码-
const num1 = 45;
const num2 = 345;
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 primeBetween = (a, b) => {
const res = [];
while(a <= b){
if(isPrime(a)){
res.push(a);
};
a++;
};
return res;
};
console.log(primeBetween(num1, num2));输出结果
以下是控制台中的输出-
[
47, 53, 59, 61, 67, 71, 73, 79, 83,
89, 97, 101, 103, 107, 109, 113, 127, 131,
137, 139, 149, 151, 157, 163, 167, 173, 179,
181, 191, 193, 197, 199, 211, 223, 227, 229,
233, 239, 241, 251, 257, 263, 269, 271, 277,
281, 283, 293, 307, 311, 313, 317, 331, 337
]热门推荐
1 医政科祝福语简短
5 贫困户祝福语简短
10 长辈钻石婚祝福语简短
11 新婚祝福语大全简短精炼
12 写给男生简短的祝福语
13 简短大气的狗年祝福语
14 下雨开工吉祥祝福语简短
15 中秋快乐祝福语简短的
16 老师祝福语简短100字
17 青春告白祖国简短祝福语
18 订婚祝福语简短好听女