11 Answers

New AnswerIt's quite simple So let us make a function for this (I will use C++, but it will be pretty much same for C) int squareSum(int num){ int sum = 0; for(int i=1; i<=num; i++){ sum = sum + i*i; } return sum; } This should work ᕙ( • ‿ • )ᕗ

Quoi Runtime I believe two loops would be 25 - 30% faster than one loop. Here is my comparison: Both solutions iterate N times, total. The double loop solution has one drawback by having one extra loop initiation. The extra loop initiation occurs only once and it is pretty quick. Otherwise, it is doing: test loop condition multiply add/store increment branch The single loop solution performs a couple extra operations every iteration. Those *extra operations occur N times. test loop condition *test index *branch [or multiply] multiply [or *branch] add/store increment branch The two-loop solution performs 5 operations for every 7 of the one-loop solution. That is 28% faster.

Brian Thanks a whole lot for that, but should I be caring about such minor details while actually creating stuff? I like to make my code more efficient, but also don't wanna type a lot

Quoi Runtime The best method would be to not use loops. We can simply use the formula for this, which is n*(n+1)*(2n+1)/6 I guess this will be the fastest method without the necessity of loops.

Quoi Runtime I admire code that is readable, follows the DRY principle, and has a good balance among memory usage, speed and compactness. Between the two approaches we are discussing, I think both of us would agree that the one-loop solution is better. It may be slower but it works with a greater range of N and generally looks better. Evaluating those low-level details may take the joy out of programming for some. That may be one of the differences between the role of a Programmer and the role of a Software Engineer. The engineer is responsible to examine wholistically how the software fits into its environment. If you are an engineer, bear in mind you don't have to play that role all the time. Do the engineering analysis when it matters. But keep enjoying the programming aspect of what you do!

Pramod Pandey I have been pondering whether there is a formula to replace looping, and I was hoping you found it. But when I tried the formula that you posted [n*(n+1)*(2*n+1)/6], it didn't match the loop result that I trust to be correct. N=42 loop result = -903 formula = 25585 N = 43 loop result = 946 formula = 27434

I thought of a cleaner-looking single loop approach: int sum = 0; //use s to provide the alternating sign +/-1 for(int i = 1, s = 1; i<=n; i++, s=-s) sum += s*i*i;

I managed to develop a formula by integration, some empirical fudging and lots of reduction. The answer consistently matches the loop result. Now we have a one-line solution! sum = (n&1? 1 : -1)*n*(n + 1)/2); Lo and behold, it is Gauss's formula for summation of consecutive integers except with alternating sign!

Anjali Rawat you could do this in two separate loops - one to sum the positive values, and one to sum the negative values; both with an increment of 2, but having different starting index values. The above approach is fast but could lead to overflow if N is large. To reduce chance for overflow, you could implement a single loop and a conditional that alternately adds or subtracts, depending on whether the index is odd or even, respectively. Here is a compact and fast implementation of the sum that checks the lowest bit of the index to determine odd versus even: sum += (i&1? i*i : -i*i);

Brian Would using two separate loops be faster than this one? - https://code.sololearn.com/cy5e2K0FcGnN/?ref=app

#include<stdio.h> int sum_of_series(int n) { int result = 0; for (int i = 1; i <= n; i++) { if (i % 2 == 0) result = result - pow(i, 2); else result = result + pow(i, 2); } return result; } int main() { int n; scanf("%d",&n); printf("%d",sum_of_series(n)); } //hope it helps :)