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); }
반응형
'Programming > Project Euler' 카테고리의 다른 글
8. Project Euler #8 : Largest product in a series. (0) | 2015.04.16 |
---|---|
7. Project Euler #7 : 10001st prime (0) | 2015.02.27 |
5. Project Euler #5 : The smallest number that can be divided by numbers below 20. (2) | 2015.01.29 |
4. Project Euler #4 : Largest palindrome product. (0) | 2015.01.22 |
3. Project Euler #3 : Find the biggest prime factor. (0) | 2015.01.20 |