+ 1
1
8. Write a JavaScript function that accepts a number as a parameter and check the number is prime or not. Note : A prime number (or a prime) is a natural number greater than 1 that has no positive divisors other than 1 and itself.
2 Antworten
+ 5
One inefficient way of doing this would be (in pseudocode, sorry):
IsPrime(x)
{
var y = 2;
while (y < x)
{
if (x % y == 0)
{
write("x is not prime");
return; // exit method
}
y++;
}
write("x is prime");
}
you could also add a check at the start of the method to make sure x > 1.
+ 1
8. Write a JavaScript function that accepts a number as a parameter and check the number is prime or not.
Note : A prime number (or a prime) is a natural number greater than 1 that has no positive divisors other than 1 and itself.
One inefficient way of doing this would be (in pseudocode, sorry):
IsPrime(x)
{
var y = 2;
while (y < x)
{
if (x % y == 0)
{
write("x is not prime");
return; // exit method
}
y++;
}
write("x is prime");
}
you could also add a check at the start of the method to make sure x > 1.