## Problem Description :

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 = 55^2 = 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?

## Concept :

This is a very simple problem. We have to apply following two properties to solve this problem mathematically.

• Sum of first N natural numbers is N(N+1)/2.
• Sum of square of first N natural numbers is N(N+1)(2N+1)/6

## Java Program :

```package com.javamultiplex.projecteuler;

/**
*
*
* @author Rohit Agarwal
* @category Project Euler Problems
* @problem Sum square difference
*
*/
public class Problem6 {

public static void main(String[] args) {

long limit = 100;
// 1^2+2^2+3^2+.....+n^2=(n*(n+1)*(2n+1))/6
long sumOfSquaresOfNaturalNumbres = (limit * (limit + 1) * ((2 * limit) + 1)) / 6;
// 1+2+3+4+...+n=(n*(n+1))/2
long sumOfNaturalNumbers = (limit * (limit + 1)) / 2;
long squareOfSumOfNaturalNumbers = (long) Math.pow(sumOfNaturalNumbers, 2);
long difference = Math.abs(squareOfSumOfNaturalNumbers - sumOfSquaresOfNaturalNumbres);
System.out.println("The sum square difference is : " + difference);

}

}

```

## Output :

 Output - Project Euler Problem 6

## References :

https://projecteuler.net/problem=6

http://www.9math.com/book/sum-first-n-natural-numbers

http://www.9math.com/book/sum-squares-first-n-natural-numbers

Thank you friends, I hope you have clearly understood the solution of this problem. If you have any doubt, suggestion or query please feel free to comment below. You can also discuss this solution in our forum.

Tags : Project Euler Problem 6 solution in Java, Sum square difference, Mathematics problems, Sum of first n natural number, sum of square of first n natural numbers.

I am simple guy with lot of ambitions. My main motive is to share whatever knowledge I have related to programming. With me you can easily learn how to solve any programming problem in Java.You can connect with me on social networking sites also.

Let's Get Connected: Linkedin | Facebook |

Project Euler | Problem 6 | Sum square difference Reviewed by Rohit Agarwal on 1/11/2017 Rating: 5

Please provide your valuable comments. If you have any suggestion please share with me I will work on it and if you have any question or doubt please ask, don't hesitate. I am your friend, i will clarify all your doubts.