JavaScript 中刚大于指定数的最小素数
我们需要编写一个 JavaScript 函数,它以内置正整数作为第一个也是唯一参数。
该函数应找到一个这样的最小素数,它仅仅大于指定为参数的数字。
例如 −
如果输入为 −
const num = 18;
则输出应为
const output = 19;
示例
以下是代码
const num = 18;
const justGreaterPrime = (num) => {
for (let i = num + 1;; i++) {
let isPrime = true;
for (let d = 2; d * d <= i; d++) {
if (i % d === 0) {
isPrime = false;
break;
};
};
if (isPrime) {
return i;
};
};
};
console.log(justGreaterPrime(num));输出
以下是控制台输出 −
19
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP