-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path006_sum_square_difference.py
35 lines (34 loc) · 1.03 KB
/
006_sum_square_difference.py
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
31
32
33
34
35
# Source
# ======
# https://www.hackerrank.com/contests/projecteuler/challenges/euler006
#
# Problem
# =======
# 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 = 30252. Hence the absolute 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 absolute difference between the sum of the squares of the first N
# natural numbers and the square of the sum.
#
# Input Format
# ============
# First line contains T that denotes the number of test cases. This is followed
# by T lines, each containing an integer, N.
#
# Constraints
# ============
# 1 <= T <= 10^4
# 1 <= N <= 10^4
#
# Output Format
# =============
# Print the required answer for each test case.
t = int(input())
for _ in range(t):
n = int(input())
square_of_sum = (n*(n+1)/2)**2
sum_of_squares = n*(n+1)*(2*n+1)/6
print(int(square_of_sum - sum_of_squares))