Skip to content
Permalink
Browse files

Reimplemented PHP solution to problem 1 in constant time.

  • Loading branch information...
chessmasterhong committed May 12, 2014
1 parent 9235cfb commit 1b9e76ee8c5af84b902399b606df5afbb8d3cb01
Showing with 5 additions and 4 deletions.
  1. +5 −4 php/p1_kevin.php
@@ -8,9 +8,10 @@
* Find the sum of all the multiples of 3 or 5 below 1000.
*/
$s = 0;
for($i = 0; $i < 1000; $i += 3) $s += $i;
for($i = 0; $i < 1000; $i += 5) $s += $i % 3 ? $i : 0;
echo $s;
// Definition of "multiple" + inclusion-exclusion principle + closed form summation (triangle numbers)
$n = 999; // "below 1000" = 1 to 999
echo (1.5 * (int)($n / 3) * ((int)($n / 3) + 1)) + (2.5 * (int)($n / 5) * ((int)($n / 5) + 1)) - (7.5 * (int)($n / 15) * ((int)($n / 15) + 1)); // O(1)
?>

0 comments on commit 1b9e76e

Please sign in to comment.
You can’t perform that action at this time.