Hey guys,
Hope you all are doing great. I am back with another problem of Project Euler on
Hope you all are doing great. I am back with another problem of Project Euler on
Project Euler #6: Sum square difference
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 N natural numbers and the square of the sum.
Input Format
First line containsT that denotes the number of test cases. This is followed by T lines, each containing an integer, N .
First line contains
Output Format
Print the required answer for each test case.
Print the required answer for each test case.
Constraints
1≤T≤104
1≤N≤104
Sample Input
2
3
10
Sample Output
22
2640
Algorithm:
For this problem, i used a straight forward approach of summing the square of numbers and squaring the sum of numbers.
Which has a time complexity of:
O(N)
It worked for this problem with the Python code and 2 of the test cases were passed.
Not a Presto!! But yes its Pythoned!!
Check my blow submission of Python Code. Do let me know if i can optimize it more.
Program:
t=input() while(t>0): t-=1 n=input() sum1 = sum([x**2 for x in xrange(1,n+1)]) sum2 = sum(xrange(1,n+1)) print sum2**2 - sum1
View comments