# Project Euler #6: Sum square difference

# Project Euler #6: Sum square difference

akisonlyforu + 8 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.

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

1512051TT2018 + 0 comments Bravo!

I_Am_True + 2 comments *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

aniruddh_sharma1 + 0 comments that is an amazing approach using GP ..thanx man : )

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.

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 + 1 comment just make n of type "long int" and use earlier expression.

arstheking97 + 0 comments but why does after doing this all testcases passes ?

Sort 196 Discussions, By:

Please Login in order to post a comment