Task. Consider the divisors of the number 30: 1, 2, 3, 5, 6, 10, 15, 30. You can see that for each divider d, the following holds: d + 30 / d is a prime number. For example, d = 2: 2 + 30/2 = 17 - simple
Find the sum of all natural numbers n not exceeding 100,000,000 such that for any divisor d of number n, the number d + n / d is prime. (i.e. the sum of such numbers that have properties like the number 30 from the example above)
Here is what I wrote:
function result($i, $num) { return $i + $num / $i; //Вычисляю } function isPrime($num) { if (gmp_prob_prime($num) == 2) //Проверяю, является ли простым return true; } $finish = 6; //Конечное число $data = []; $numbers = []; for ($NUMBER = 1; $NUMBER <= $finish; $NUMBER++) { for ($i = 1; $i <= $NUMBER; $i++) { $result = result($i, $NUMBER); if (is_integer($result) && isPrime($result)) { $data[] = $i; $numbers[] = $NUMBER; } } } foreach ($numbers as $num) { echo $num; } echo "<br>"; foreach ($data as $nums) { echo $nums; } In the case of 6, 1, 2, and 6 are suitable. We must exclude 4 and all similar numbers for which not all divisors are suitable for this condition.