Project Euler Problem #6 - Sum Square Difference (in Java)

  1. public class Problem_6_Sum_Square_Difference {

  2.     /*
  3.         The sum of the squares of the first ten natural numbers is,
  4.         12 + 22 + ... + 102 = 385
  5.                
  6.         The square of the sum of the first ten natural numbers is,
  7.         (1 + 2 + ... + 10)2 = 552 = 3025
  8.                
  9.         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.

  10.         Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum.
  11.     */
  12.        
  13.     public static void main (String[] args) {
  14.                
  15.         int sumSquares = 0, sum = 0, difference = 0;
  16.                
  17.         for (int i = 1; i <= 100; i++) {
  18.             sum += i;
  19.             sumSquares += (int)Math.pow(i, 2);
  20.         }
  21.                
  22.         difference = (int)Math.pow(sum, 2) - sumSquares;
  23.         System.out.println(difference);
  24.     }
  25. }
DOWNLOAD

              Created: February 10, 2014
Completed in full by: Michael Yaworski