Skip to content

Instantly share code, notes, and snippets.

@drupol
Created September 14, 2019 16:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save drupol/f07e88ba2d4990818a2db9a072f00bd6 to your computer and use it in GitHub Desktop.
Save drupol/f07e88ba2d4990818a2db9a072f00bd6 to your computer and use it in GitHub Desktop.
Collatz conjecture's sequence of numbers
<?php
declare(strict_types=1);
include 'vendor/autoload.php';
use drupol\collection\Collection;
// The Collatz conjecture (https://en.wikipedia.org/wiki/Collatz_conjecture)
$collatz = static function (int $initial = 1): int
{
return 0 === $initial % 2 ?
$initial / 2:
$initial * 3 + 1;
};
Collection::iterate($collatz, 10)
->until(static function ($number): bool {
return 1 === $number;
})
->all(); // [5, 16, 8, 4, 2, 1]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment