본문 바로가기
Programming/Project Euler

6. Project Euler #6 : Sum square difference

by 작은별하나 2015. 2. 13.

In fact, this problem is equivalent to the previous problem #1, to obtain the sum. (http://odev.tistory.com/8)


As you know, you can get the answer using for-loop.  I think that you'd better to use summation fomula.




Using fomula is more efficient to use for-loop, as you know already.  The performance betwwen them, bigger range for sum larger performance difference is.

int main()
{
        int n = 100;
        int s1, s2;

        s1 = n*(n+1)*(2*n+1)/6;
        s2 = n*(n+1)/2;
        printf("Ans = %d\n", s2*s2 - s1);
}


댓글