Skip to content

Instantly share code, notes, and snippets.

@m4grio
Last active February 27, 2017 08:56
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 m4grio/668faa83092bb8887482479f3bb44807 to your computer and use it in GitHub Desktop.
Save m4grio/668faa83092bb8887482479f3bb44807 to your computer and use it in GitHub Desktop.
PHP vs Erlang
-module(number).
-export([
is_perfect/1
]).
is_perfect(N, N, S) -> N==S;
is_perfect(N, M, S) when N rem M == 0 -> is_perfect(N, M+1, S+M);
is_perfect(N, M, S) -> is_perfect(N, M+1, S).
is_perfect(N) -> is_perfect(N, 1, 0).
<?php
/**
* @param int $number
*
* @return bool
*/
function isPerfect(int $number): bool
{
for ($n = 2; $n <= sqrt($number); $n++) {
if (!($number % $n)) {
$d += $n;
if ($n <> $number / $n) {
$d += $number / $n;
}
}
}
return ++$d == $number;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment