Click here to Skip to main content
15,898,035 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
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;
}
Posted
Updated 1-Aug-18 4:04am

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]
 
Share this answer
 
v4
Comments
Richard Deeming 3-Aug-18 15:12pm    
Technically, it's the remainder operator, not the modulus operator. :)

What’s the difference? Remainder vs Modulus – Fabulous Adventures In Coding[^]
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.

JavaScript
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.
 
Share this answer
 
v2
As you already tested for factor 2, you don't need to test other even numbers.
Replace
JavaScript
for(let i = 3; i < n - 1; i++){

with
JavaScript
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
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900