这个条件在Prime函数中的输出是什么? [英] What is the output of this condition in the Prime Function?

查看:150
本文介绍了这个条件在Prime函数中的输出是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从另一篇文章中发现了这段代码,我试图理解这个解决方案的一部分。

 
($)

 函数isPrime(num){
for( var i = 2; i< num; i ++){
if(num%i === 0){
return false;
}
}
返回true;
}

尤其是这条线

  num%i === 0 

什么我可以理解的是, arr 是一个数组,它在2之后产生每个奇数(例如,[2,3,5,7,9,11,13,15, 17])。然后每个数字通过函数isPrime 运行。我试图理解的是 num%i === 0 有关的输出是什么(var i = 2; i < num; i ++)



    是这样的输出吗?
    li 3(num)%2(i)
    li 5(num)%3(i)
  • 7(num )%4(i)
    li 9(num)%5(i)
    li 11(num)%6(i)


解决方案



  console.log(`$ {num}%$ {i} === $ {num%i}`); //额外行
if(num%i === 0){
return false;




$ b $ p
$ b

在这段代码中,代码实际上做的是它会经过 2 num 之间的每一个数字,将它赋值给变量 i ,并检查 num 是否可被 i 整除。如果是,那么它将返回 false



函数(称为模函数)基本上取两个数字,并返回除以第二个数字后的第一个数字的余数。举例来说:

  5%2 // = 1,5/2 = 2 with 1超过
7%3 // = 1,7/3 = 2还剩1美元b $ b

如果余数是 0 ,那么显然第一个数字可以被第二个数字整除,因为没有什么可以留下。因此 num%i === 0 正在检查可分性,本质上它检查 num 可被 i整除。

例如,当检查 5 (带有额外的 console.log 行),这是输出到控制台的内容:



< pre class =lang-js prettyprint-override> 5%2 === 1
5%3 === 2
5%4 === 1

这就是 6 输出为 num

  6%2 === 0 

(已停止,因为 false 被返回。)


I found this code from another post and I'm trying to understand a part of this solution.

function sumPrimes(n) {
  function isPrime(num) {
    for ( var i = 2; i < num; i++ ) {
        if ( num % i === 0 ) {
            return false;
        }
    }
    return true;
}
    var arr = 2;
    for ( var i = 3; i <= n; i+=2 ) {
        if ( isPrime(i) ) {
            arr += i;
        }
    }
return arr;
}
console.log(sumPrimes(10));

The part I'm asking about is this particular function

function isPrime(num) {
        for ( var i = 2; i < num; i++ ) {
            if ( num % i === 0 ) {
                return false;
            }
        }
        return true;
    }

especially this line in question

num % i === 0

What I can understand is that arr is an array that product every odd number after 2 (for example, [2, 3, 5, 7, 9, 11, 13, 15, 17]). Then every number is run through function isPrime. What I'm trying to understand is what are the outputs of num % i === 0 in relation to for ( var i = 2; i < num; i++ )?

Are the outputs like this?

  • 3(num) % 2(i)
  • 5(num) % 3(i)
  • 7(num) % 4(i)
  • 9(num) % 5(i)
  • 11(num)% 6(i)

解决方案

for ( var i = 2; i < num; i++ ) {
    console.log(`${num} % ${i} === ${num % i}`); // Extra line
    if ( num % i === 0 ) {
        return false;
    }
}

In this code, what the code is actually doing is it's going through every single number between 2 and num, assigning it to the variable i, and checking if num is divisible by i. If it is, then it'll return false.

The % function (called the modulo function), basically takes two numbers, and returns the first number's remainder when divided by the second number. So, for example:

5 % 2 // = 1, 5/2 = 2 with 1 left over
7 % 3 // = 1, 7/3 = 2 with 1 left over

If the remainder is 0, then obviously the first number is divisible by the second number, since there's nothing left over. So the line num % i === 0 is checking for divisibility, essentially - it's checking if num is divisible by i.

For example, when checking 5 (with the extra console.log line), this is what's outputted to the console:

5 % 2 === 1
5 % 3 === 2
5 % 4 === 1

And this is what's outputted with 6 as num:

6 % 2 === 0

(It's stopped, because false is returned.)

这篇关于这个条件在Prime函数中的输出是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆