## Hackerrank – Problem description

The problem description – Hackerrank.

## Solution

We calculate `3`

equations for every number from `1`

to `N`

.

Sum of squares:

Square of sum:

**Result** – Difference between sums:

I created solution in:

All solutions are also available on my GitHub profile.

## Java

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
import java.util.Scanner; public class SumSquareDifference { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int tests = Integer.parseInt(scanner.nextLine()); for (int i = 0; i < tests; i++) { int number = Integer.parseInt(scanner.nextLine()); System.out.println(sumDifference(number)); } scanner.close(); } private static long sumDifference(int number) { long squareSum = 0; long sumSqares = 0; for (int i = 1; i <= number; i++) { squareSum += i; sumSqares += (i * i); } squareSum *= squareSum; return squareSum - sumSqares; } } |