# Project Euler #6: Sum square difference

# Project Euler #6: Sum square difference

akisonlyforu + 6 comments If your solution is not working for TestCase 1 , please make sure that the input type is of long type.

midnjerry + 0 comments I would just like to add in Java - the compiler treats all nontype numerals as int first.

So 3 + 1 <==== is treated as int first and foremost.

long result = 3 + 1 <--- is int THEN is converted to long after the sum is calculated.

long result = Integer.MAX_VALUE + 1; //will produce: -2147483648

Thanks for the tip. I couldn't figure out why changing the input type from int to long changed my output! Now everything works. Thanks!

EDIT: BTW This isn't a bug. I took the Java certification class, and this is part of the compiler knowledge needed to pass the test. I just forgot about it :(

warrior_ + 1 comment Please Just read these 2 formulas , After u can try

1) sum of first n natural numbers is = n*(n+1)/2

2) sum of first n natural numbers idiviual squares is : : n*(n+1)*(2*n+1)/6

`ex: 1^2+2^2+3^2+.......+n^2`

pypypypypy3 + 0 comments I simplified and I got (3*n^4 + 2*n^3 - 3*n^2 - 2*n)/12

sakshi1995 + 0 comments thanks ,i was annoyed at first coz it wasnt working.

Stephan_R + 0 comments thanks it was helped me

nehapendem6 + 0 comments the input number? it's still showing the same plz help

thecoducer + 0 comments Can you kindly explain why it is needed to use long instead of int? My code justr works fine after I used long. But I'm wondering why hasn't int datatype helped me? N<=10^4. If the value of N is 10000, int can store ir. Right?

[deleted] + 1 comment Makes the problem too easy.

1512051TT2018 + 0 comments Bravo!

cvipin501 + 3 comments My earlier submissions were failing test case 2. So I broke my expression for s2=(n*(n+1)*((2*n)+1))/6, into a series of operations. Then my code passed all test cases. What changed?

int main(){ int t; cin >> t; for(int a0 = 0; a0 < t; a0++){ int n; cin >> n; long int s1=(n*(n+1))/2; s1=s1*s1; long int s2; s2=n*(n+1); int a=2*n+1; s2=s2*a; s2=s2/6; cout<<s1-s2<<endl; } return 0; }

vivekramnani + 0 comments [deleted]kapil18pandey + 0 comments I faced the same problem in java and by this approach I cleared 2nd test case.How does breaking the code in this way make a difference?

vikumsw + 0 comments just make n of type "long int" and use earlier expression.

I_Am_True + 1 comment *python 3 passed all test cases*import sys t=int(input()) while t>0: n=int(input()) j=n*(n+1)*(2*n+1)//6 m=n*(n+1)//2 m=m*m print(m-j) t -=1

Atul_Anand_Jha + 1 comment how does that even worked?

Stynx + 0 comments n*(n+1)*((2*n)+1)//6 is the formula for the sum of n perfext squares

NeilA + 1 comment There is a bug with the C code,

`n`

has to be changed to`unsigned long long`

to pass testcase 1 because otherwise it would be overflowing for some reason.thebongy + 0 comments Here is a brief introspection on what the issue

*could be*:Let's say you have a variable c of type

**long long**and variables a and b of type**int**. Now, if you type:c = a*b

The expression on the RHS is

**evaluated first with type int**, and then the**result of that is casted to long long**. But this is far too late, as the overflow with ints has already occured (if any), and then the overflowed answer is converted to long long. Instead, what you probably meant to be doing is:c = static_cast<long long>(a)*static_cast<long long>(b)

As a side note, this is probably a pain to type in a longer expression, and therefore it is better to just declare N as long long initially, thus ensuring all arithmetic expressions are evaluated as long long.

BalaKrishnanR + 0 comments The required expression is,

which is,

The expression can be recursively calculated as,

Using sum of consecutive natural numbers,

It can also be made efficient by caching computed values (, so a cache with a size of should be sufficient). In Python, it is even simpler with,

import functools @functools.lru_cache(maxsize = 10000) def SumSquaredifference(n): if(n == 1): return 0 return SumSquaredifference(n-1)+((n**2)*(n-1))

aaha97 + 0 comments i think it is worth mentioning that i could not recall the formulas for sum of squares for natural number. So i tried to play around with multinomial expansion and I got to a solution that performed better than the formula based solution using O(n) memory!

https://trans4mind.com/personal_development/mathematics/series/multiNomialExpansion.htm

TheLordkiller + 1 comment Simple C++ Solution

#include <bits/stdc++.h> using namespace std; int main() { long long t, n; cin >> t; for (int q = 0; q < t; ++q) { cin >> n; cout << n * (n + 1) * (3 * n * n - n - 2) / 12 << "\n"; } return 0; }

akshitdesai + 0 comments [deleted]

arnablayekju + 0 comments Easiest among 1 to 6.

sohail_shaukat_ + 0 comments Heres my code with derivation link below if anyone wants to check it out:

#!/bin/python3 t = int(input().strip()) for a0 in range(t): n = int(input().strip()) square_of_sum = ((n * (n+1))// 2) ** 2 sum_of_squares = (((n) * (n+1) * (2*n+1)) // 6) print(square_of_sum-sum_of_squares)

Sort 175 Discussions, By:

Please Login in order to post a comment