# Project Euler | Problem 6 | Sum square difference

##
__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__ :

##
__References__ :

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.**

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

## No comments:

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.