Skip to content

Instantly share code, notes, and snippets.

@fakeheal
Last active April 14, 2016 07:48
Show Gist options
  • Save fakeheal/ea9c42592db83aad2bba to your computer and use it in GitHub Desktop.
Save fakeheal/ea9c42592db83aad2bba to your computer and use it in GitHub Desktop.
PHP: Highly divisible triangular number (ProjectEuler Problem #12)
<?php
function countDivisors($number)
{
$count = 0;
$sqrtOfNumber = (int)sqrt($number);
for($i = 1; $i <= $sqrtOfNumber; $i++)
{
if($number % $i == 0)
$count += 2;
}
return sqrt($sqrtOfNumber) == $number ? $count-- : $count;
}
$n = 0;
$i = 1;
while(countDivisors($n) < 500){
echo 'Checking: '.$n." \n";
$n += $i;
$i++;
}
echo $n;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment