Statistics

Content View Hits : 21521
Sum of squares without multiplying PDF Print E-mail
User Rating: / 0
PoorBest 
Written by Gurito   

This was a program we had to implement at the university a couple of months ago... It's just a tiny piece of code showing how to perform the sum of the squared numbers in [0, N]. That means, 1^2 + 2^2 + ... + N^2.

It's written in c and, well.. you'll find it pretty straight forward.

  1. /* This program calculates the sum of the squared numbers in
  2.  * the interval [0,N] without doing any multiplication.
  3.  *
  4.  * You may use/change the code any way you want.
  5.  * This program was made with academic purposes.
  6.  *
  7. */
  8.  
  9. #include <stdio.h>
  10.  
  11. int main (argsc, argsv)
  12. int argsc;
  13. char* argsv[];
  14. {
  15.     int N = 0;
  16.     for(N=1; N<11; N++)
  17.     {
  18.         int x = 0, c = 0, d = 1, e = 3;
  19.         while (x<N)
  20.         {
  21.             x = x + 1;
  22.             c = c + d;
  23.             d = d + e;
  24.             e = e + 2;           
  25.         }
  26.         printf("The sum of the squares is: %i\n",c);
  27.     }
  28.     return 0;
  29. }
 

Last Updated on Tuesday, 31 March 2009 15:13