How to check whether a number is prime or not?
Answer / Suresh Kumar Patel
A prime number is a positive integer greater than 1 that has no positive divisors other than 1 and itself. Here's a simple PHP function for checking if a given number is prime:
```php
function is_prime($num) {
if ($num <= 1) {
return false;
}
for ($i = 2; $i < sqrt($num); $i++) {
if ($num % $i === 0) {
return false;
}
}
return true;
}
```
This function checks all numbers up to the square root of the input number (since a larger factor of the number would be smaller than its square root) and returns `true` if the number is prime, otherwise `false`.
| Is This Answer Correct ? | 0 Yes | 0 No |
What are Sub routines?
Does strlen include null?
How would you impletement download and upload a file in php
Explain the difference between array_merge() and array_combine()?
How do you destroy a session?
Which functions are used to count the total number of array elements in php?
What are the characteristics of php variables?
What is escape data in php?
Is age an interval or ratio?
in PHP for pdf which library used?
How to remove the new line character from the end of a text line?
Tell us how can we access the data sent through the url with the post method?