Skip to content

Instantly share code, notes, and snippets.

@drupol
Last active June 27, 2021 16:20
Show Gist options
  • Select an option

  • Save drupol/e42ac5106fa38973ef871338cb1b1d96 to your computer and use it in GitHub Desktop.

Select an option

Save drupol/e42ac5106fa38973ef871338cb1b1d96 to your computer and use it in GitHub Desktop.
<?php
// Factorial of a number
printf("\nype a number: ")
&& ($g = (int) trim(fgets(STDIN))) && (fn(int $g, $y) =>
printf($y(static fn (callable $function) => static fn ($n = 1) => (1 >= $n) ? 1 : $n * $function($n - 1))($g))
)(
$g,
static fn (callable $g): callable => (static fn ($f): callable => $f($f))(static fn ($f): callable => $g(static fn ($x) => $f($f)($x)))
);
// Collatz/Syracuse conjecture
printf("\nType a number: ")
&& ($g = (int) trim(fgets(STDIN))) && (fn($g, $y) =>
$y(static fn (callable $loop) => static fn (int $n) => printf('%s,', $n) && (1 === $n ? 1 : $loop(0 === $n % 2 ? $n / 2 : $n * 3 + 1)))($g)
)(
$g,
static fn (callable $g): callable => (static fn ($f): callable => $f($f))(static fn ($f): callable => $g(static fn ($x) => $f($f)($x)))
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment