for testing and deploying your application
for finding and fixing issues
for empowering human code reviews
<?php
declare(strict_types=1);
namespace drupol\phpermutations\Iterators;
use drupol\phpermutations\Iterators;
use const PHP_INT_MAX;
class Perfect extends Iterators
{
/**
* The maximum limit.
*
* @var int
*/
protected $max;
* The minimum limit.
protected $min;
* Perfect constructor.
public function __construct()
$this->setMaxLimit(PHP_INT_MAX);
$this->setMinLimit(2);
parent::__construct([], null);
}
* {@inheritdoc}
public function current(): mixed
for ($i = $this->key(); $this->getMaxLimit() > $i; ++$i) {
if ($this->isPerfectNumber($i)) {
$this->key = $i;
return $i;
return $this->getMaxLimit();
* Get the maximum limit.
* @return int
* The limit
public function getMaxLimit(): int
return (int) $this->max;
* Get the minimum limit.
public function getMinLimit(): int
return 2 > $this->min ? 2 : $this->min;
* @return void
public function next(): void
++$this->key;
public function rewind(): void
$this->key = $this->getMinLimit();
* Set the maximum limit.
* @param int $max
public function setMaxLimit($max): void
$this->max = $max;
* Set the minimum limit.
* @param int $min
public function setMinLimit($min): void
$this->min = $min;
* @return bool
public function valid(): bool
return $this->current() < $this->getMaxLimit();
* Test if a number is perfect or not.
* Source: http://iceyboard.no-ip.org/projects/code/php/perfect_number/
* @param int $number
* The number to test
* The true if the number is perfect, false otherwise
protected function isPerfectNumber($number): bool
$d = 0;
$max = sqrt($number);
for ($n = 2; $n <= $max; ++$n) {
if (!($number % $n)) {
$d += $n;
if ($number / $n !== $n) {
$d += $number / $n;
return ++$d === $number;