Skip to content

Instantly share code, notes, and snippets.

@pmgupte
Last active December 28, 2015 18:09
Show Gist options
  • Select an option

  • Save pmgupte/7541367 to your computer and use it in GitHub Desktop.

Select an option

Save pmgupte/7541367 to your computer and use it in GitHub Desktop.
PHP solution to Project Euler problem # 6.
<?php
/**
* Problem 6: Sum square difference
*
* The sum of the squares of the first ten natural numbers is,
* 1^2 + 2^2 + ... + 10^2 = 385
* The square of the sum of the first ten natural numbers is,
* (1 + 2 + ... + 10)^2 = 552 = 3025
* Hence the difference between the sum of the squares of the first ten natural numbers and the square of the sum is 3025 − 385 = 2640.
*
* Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum.
*/
$sum = 0;
$square = 0;
for ($i=1; $i<=100; $i++) {
$sum += $i*$i;
$square += $i;
}
echo "diff between sum of squares and square of sum of first 100 natural numbers = ", ($square*$square) - $sum;
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment