Home  >  Article  >  Backend Development  >  PHP gets the number divisible by 1 and itself

PHP gets the number divisible by 1 and itself

WBOY
WBOYOriginal
2016-08-23 09:17:511295browse

php gets the number within 89 that is divisible by 1 and itself. I can’t find a clue about this, so I’m looking for methods and guidance

Reply content:

php gets the number within 89 that is divisible by 1 and itself. I can’t find a clue about this, so I’m looking for methods and guidance

I have encountered this problem. I remember I encountered it when I went to a company for an interview. I specially compiled it for you to refer to and it will be easy to understand.

<code>final function getPrimesNumber($number) {
    $primes = array ();
    for($i = 1; $i < $number; $i ++) {
        for($j = 2; $j < $i; $j ++) {
            if ($i % $j == 0) {
                continue 2;
            }
        }
        $primes [] = $i;
    }
    return $primes;
}</code>

It isonlya number that can be divisible by 1 and itself, that is, a prime number, right?

<code>$max = 89;

for($i = 1; $i <= $max; $i++) {
    $k = 0;
    for($j = 1; $j < $i; $j++) {
        if($i % $j == 0) {
           $k++; 
        }
    }
    if($k == 1) {
        echo $i . '\n';
    }
}</code>
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn