PHP gets the number divisible by 1 and itself

WBOY
Release: 2016-08-23 09:17:51
Original
1317 people have browsed it

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>
Copy after login

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>
Copy after login
Related labels:
php
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template