结果是什么(n%2) [英] What is outcome of (n%2)

查看:84
本文介绍了结果是什么(n%2)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

先生,我很困惑!(n%2)这是什么结果。我知道这是真的还是假的但是(n%2)的结果是什么帮助我完全混淆



我尝试了什么:



我看到别人的代码,他分配了一个有参数n&这是初号的测试。

代码是:



让isPrime = function(n){

if(n === 2){

返回true;

}

if((n< 2)||!(n %2)||!Number.isInteger(n)){

返回false;

}



for(let i = 3; i< n - 1; i ++){

if(n%i === 0){

return false;

}

}

返回true;

}

解决方案

< blockquote>它不是true或false它是0或1:%是模数运算符,当左侧被右边分割时返回余数。并且由于任何除以2的东西只能有零或一的余数,这就是在这种情况下返回的内容。



如果你使用10而不是模数二,它将返回该数字的最低有效位:12345%10将给你5.



错别字。很多错别字[/ edit]


这是一个有趣的问题,有一个有趣的素数函数。

稍微改变代码,你可以看到n的值%i每次循环并了解它是如何工作的。

你需要做的就是在for循环中添加一行:

console.log(n =+ n +:+ n% i);

实际上,这条线更好(更清楚地显示输出)

 console.log(n +% + i +=+ n%i); 



它将为isPrime(7)提供以下内容;

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



我也更新了jsbin。



现在,当你运行它时,每次循环都会看到n%i的值。



  isPrime =  function (n){
if (n === 2 ){
return true ;
}
if ((n< 2 )||!(n %2)||!Number.isInteger(n)){
return false ;
}
for let i = 3 ; i< n - 1 ; i ++){
console .log(n + + i + = + n%i);
if (n%i === 0 ){

return false ;
}
}
返回 true ;
}





这是我创建的jsbin的链接: JS Bin - 协作式JavaScript调试 [ ^ ]

当你点击Run with JS按钮时,你可以去那里看看isPrime(7)。

你需要打开浏览器的控制台窗口(在大多数浏览器中为F12)以查看console.log输出。


正如您已经测试了因子2,您不需要测试其他偶数。

替换

   i =  3 ; i< n  -   1 ; i ++){



with

  for  i =  3 ; i< n  -   1 ; i + =  2 ){



,你快两倍。

设置在n的平方根处的循环结束,也将显着提高速度。

对于10000左右的素数

你的例程将循环大约10000次

i + = 2 ,它将循环5000次

,以平方根结束,它将循环50次


Sir, i'm confuse in !(n%2) what was the result of this . I know it's true or false but what was the outcome of (n%2) plz help i'm totally confuse

What I have tried:

I'm see the code someone else in that he assign a function that has argument n & that is tested for prime no.
code is:

let isPrime = function(n){
if(n === 2){
return true;
}
if((n < 2) || !(n%2) || !Number.isInteger(n)){
return false;
}

for(let i = 3; i < n - 1; i++){
if(n%i === 0){
return false;
}
}
return true;
}

解决方案

It's not "true" or "false" it's 0 or 1: % is a modulus operator which returns the remainder when the left side is divided by the right. And since anything divided by two can only ever have a remainder of zero or one, that is what is returned in this case.

If you use modulus with 10 instead of two, it will return the least significant digit of the number: 12345 % 10 will give you 5.

[edit] Typos. lots of typos [/edit]


This is an intriguing question with an interesting function for primes.
Alter the code slightly and you can see the value of n%i each time through the loop and learn how it works.
All you need to do is add one line in the for loop:
console.log("n = " + n + " : " + n%i);
Actually, this line is better (more clearly shows output)

console.log(n + "%"+i + " = " + n%i);


It'll give the following for isPrime(7);

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


I updated the jsbin too.

Now, when you run it you will see the value of n%i each time through the loop.

let isPrime = function(n){
if(n === 2){
return true;
}
if((n < 2) || !(n%2) || !Number.isInteger(n)){
return false;
}
for(let i = 3; i < n - 1; i++){
console.log(n + "%"+i + " = " + n%i);
if(n%i === 0){

return false;
}
}
return true;
}



Here's a link to a jsbin I created : JS Bin - Collaborative JavaScript Debugging[^]
You can go there and see isPrime(7) run when you click the Run with JS button.
You need to open the browser's console window (F12 in most browsers) to see the console.log output.


As you already tested for factor 2, you don't need to test other even numbers.
Replace

for(let i = 3; i < n - 1; i++){


with

for(let i = 3; i < n - 1; i+=2){


and you are twice as quick.
setting the end of loop at square root of n, will also dramatically improve the speed.
For a prime around 10000
your routine will loop about 10000 times
with the i+=2, it will loop 5000 times
with endding at square root, it will loop 50 times


这篇关于结果是什么(n%2)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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