How to resolve the algorithm Proper divisors step by step in the PHP programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Proper divisors step by step in the PHP programming language
Table of Contents
Problem Statement
The proper divisors of a positive integer N are those numbers, other than N itself, that divide N without remainder. For N > 1 they will always include 1, but for N == 1 there are no proper divisors.
The proper divisors of 6 are 1, 2, and 3. The proper divisors of 100 are 1, 2, 4, 5, 10, 20, 25, and 50.
Show all output here.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Proper divisors step by step in the PHP programming language
Explanation:
This PHP code defines a function called ProperDivisors
that generates all the proper divisors of a given number n
. Proper divisors are the positive divisors of n
that are less than n
itself.
Function Implementation:
- The function uses a generator to yield (output) the divisors one by one.
- It starts by yielding 1, which is a proper divisor of every number.
- It then iterates over the range [2, sqrt(n)].
- For each
i
in this range, it checks ifn % i == 0
. Ifi
dividesn
evenly, it yieldsi
and also addsn / i
to a list of large divisors ifi
is not the square root ofn
. - After the loop, it iterates over the large divisors in reverse order and yields them.
Assertions and Example Output:
- The code includes an assertion that verifies the correctness of the function for
n = 100
. - It also prints the proper divisors of numbers from 1 to 10.
Counting Divisors:
- The code initializes an array called
$divisorsCount
. - It iterates over the range [1, 20000) and counts the number of proper divisors for each number
i
. - It stores the count along with the numbers having that count in the
$divisorsCount
array. - Finally, it sorts
$divisorsCount
by the number of divisors and prints the numbers with the most divisors and their count.
Source code in the php programming language
<?php
function ProperDivisors($n) {
yield 1;
$large_divisors = [];
for ($i = 2; $i <= sqrt($n); $i++) {
if ($n % $i == 0) {
yield $i;
if ($i*$i != $n) {
$large_divisors[] = $n / $i;
}
}
}
foreach (array_reverse($large_divisors) as $i) {
yield $i;
}
}
assert([1, 2, 4, 5, 10, 20, 25, 50] ==
iterator_to_array(ProperDivisors(100)));
foreach (range(1, 10) as $n) {
echo "$n =>";
foreach (ProperDivisors($n) as $divisor) {
echo " $divisor";
}
echo "\n";
}
$divisorsCount = [];
for ($i = 1; $i < 20000; $i++) {
$divisorsCount[sizeof(iterator_to_array(ProperDivisors($i)))][] = $i;
}
ksort($divisorsCount);
echo "Numbers with most divisors: ", implode(", ", end($divisorsCount)), ".\n";
echo "They have ", key($divisorsCount), " divisors.\n";
You may also check:How to resolve the algorithm Multifactorial step by step in the FreeBASIC programming language
You may also check:How to resolve the algorithm Fibonacci sequence step by step in the Joy programming language
You may also check:How to resolve the algorithm Hilbert curve step by step in the Rust programming language
You may also check:How to resolve the algorithm String append step by step in the C programming language
You may also check:How to resolve the algorithm General FizzBuzz step by step in the Nanoquery programming language