adspace


How to check whether a number is prime or not?

Answer Posted / 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



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Explain me is it possible to destroy a cookie?

1025


List some features of php that are deprecated in php7?

1021


How to create a web form?

1109


Tell me what kind of things have you done on the social side?

1064


What is the current stable version of php? What advance thing in php7?

1094


How can we extract string "pcds.co.in" from a string "https://info@pcds.co.in" using regular expression of php? More on reg can you explain

1077


sort term descripttion form, report and uery

2254


Write a program using while loop?

1115


What is the difference between htmlentities() and htmlspecialchars()?

1090


if you run the app program all vendor open items are cleared but is it possible to reverse the again again open items please tell me the answer

1988


hello all, I need some sample placement papers in lion bridge.. can anyone help me?

2175


What is trait in php?

1065


how to detect a mobile device using php

1173


What sized websites have you worked on in the past?

1085


What is difference between static and final in php?

1108