Problem 6: Sum square difference

*Difficulty: Easy

Problem:

The sum of the squares of the first ten natural numbers is,

12 + 22 + … + 102 = 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.

How to solve:

This problem is so damn easy.

Implementation (Java):

Solution took 1ms

public class Prob6 {

        public static void main(String[] args) {

                long cur = System.currentTimeMillis();

                int sumSquare = 0;

                int squareSum = 0;

                for(int i = 1; i <= 100; ++i) {

                        sumSquare+= (int)Math.pow(i,2);

                        squareSum+= i;

                }

                squareSum= (int)Math.pow(squareSum, 2);

                System.out.println(squareSumsumSquare);

                System.out.println(“Took “ + (System.currentTimeMillis()cur) + “ms”);

        }

}

Advertisements