用JS检查是否是素数

发布于 2025-01-11 13:30:05 字数 389 浏览 0 评论 0原文

我首先尝试仅将素数(不含 2)推入数组,然后将它们全部求和,但未定义。

我已经为此工作了很长时间,如果有人能帮助我,我将不胜感激。

let letsCheck = () => {

    let ourList = []
    let sum = 0

    for(let i = 2; i <= 50; i++) {
        if(i % 2 !== Number.isInteger()) {
            ourList.push(Number(i))
        }           
    }
    
    for(let prime in ourList) {
        sum += ourList[prime]
    }
}

I'm first trying to push only prime numbers (without 2) to an array and then sum them all but getting undefined.

I've been working on this for long days, I'd appreciate if anyone could help me.

let letsCheck = () => {

    let ourList = []
    let sum = 0

    for(let i = 2; i <= 50; i++) {
        if(i % 2 !== Number.isInteger()) {
            ourList.push(Number(i))
        }           
    }
    
    for(let prime in ourList) {
        sum += ourList[prime]
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

慕巷 2025-01-18 13:30:05

首先,您不是在检查素数,而是通过 % 运算符检查奇数。

其次,您正在检查 Number.isNumber 函数,该函数将返回布尔值,因此比较存在一些问题。

这是一种可能有帮助的解决方案。

let letsCheck = () => {

    let ourList = []
    let sum = 0

    for(let i = 3; i <= 50; i++) {
        if(isPrimeNumber(i)) {
            ourList.push(Number(i))
        }           
    }
    
    for(let prime in ourList) {
        sum += ourList[prime]
    }
}

const isPrimeNumber = number => {
    for(let i = 2; i <= Math.ceil(number/2); i++) {
        if(number % 2 === 0) {
            return false;
        }
    }

    return true;
}

First of all, You are not checking prime but checking odd numbers by % operator.

Second, you are checking Number.isNumber function which will return the boolean so, the comparison have some issues.

Here is one solution which may help.

let letsCheck = () => {

    let ourList = []
    let sum = 0

    for(let i = 3; i <= 50; i++) {
        if(isPrimeNumber(i)) {
            ourList.push(Number(i))
        }           
    }
    
    for(let prime in ourList) {
        sum += ourList[prime]
    }
}

const isPrimeNumber = number => {
    for(let i = 2; i <= Math.ceil(number/2); i++) {
        if(number % 2 === 0) {
            return false;
        }
    }

    return true;
}
把梦留给海 2025-01-18 13:30:05

从您的代码中,更有可能获得奇数/偶数而不是质数。

素数是大于 1 的整数,只有两个因数 - 1 和数字本身

奇数是指不以 2 为因数的数字,除以 2 后余数为 1。

然后,作为基本的编程数学,mod 的工作方式类似于乘法/加法/减法如果两个运算符/数字都是整数,则结果将为整数mod 运算基本上是为了获得除法的余数,即 5 / 2 = 2,余数 = 1,因此 5 % 2 = 1代码>.

并且,在循环中,i 已经是一个数字,因此推送 Number(i) 相当于单独推送 i。如果您只想获得总和,则不需要该数组,只需将其删除即可。您可以通过将其累加到 sum 变量中来获得总和。

因此,如果您希望获得[2,50]范围内的奇数之和,则应该是:

let letsCheck = () => {

    let sum = 0

    for(let i = 2; i <= 50; i++) {
        if(i % 2 !== 0) {
            sum += i;
        }           
    }
    
    console.log(sum);
}

letsCheck();

如果你想得到 0 到 50 之间除 2 之外的质数,应该是:

function isPrimeExclude2(num) {
  if(num <= 2) return false;
  for(let i=2; i*i <= num; i++){
    if (num % i == 0) return false;
  }
  return true;
}

let letsCheck = () => {

    let sum = 0

    for(let i = 2; i <= 50; i++) {
        if(isPrimeExclude2(i)) {
            sum = sum + i;
        }           
    }
    console.log(sum);
}



letsCheck();

From, your code, it was more likely for obtaining odd/even numbers instead of prime numbers.

Prime numbers are whole numbers greater than 1, that have only two factors – 1 and the number itself

Odd numbers are the numbers that doesn't have 2 as its factor, and will have remainder = 1 if it gets divided by 2.

Then, as the basic programming math, the mod works like multiplication/add/subtraction that if both operators/numbers are Integer, the result would be Integer. The mod operation is basically for obtaining the remainders from the division, i.e. 5 / 2 = 2, with remainders = 1, thus 5 % 2 = 1.

And, in the looping, the i is already a number, so pushing the Number(i) is equivalent with pushing i alone. If you just want to get the sum, the array is not necessary there and should be just removed. You can get the sum by accumulate it into the sum variable.

Thus, if you wish to get the sum of odd numbers in the range [2,50], it should be:

let letsCheck = () => {

    let sum = 0

    for(let i = 2; i <= 50; i++) {
        if(i % 2 !== 0) {
            sum += i;
        }           
    }
    
    console.log(sum);
}

letsCheck();

And if you wish to get the prime numbers from 0 to 50 excluding 2, it should be:

function isPrimeExclude2(num) {
  if(num <= 2) return false;
  for(let i=2; i*i <= num; i++){
    if (num % i == 0) return false;
  }
  return true;
}

let letsCheck = () => {

    let sum = 0

    for(let i = 2; i <= 50; i++) {
        if(isPrimeExclude2(i)) {
            sum = sum + i;
        }           
    }
    console.log(sum);
}



letsCheck();

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文