Skip to main content
Rollback to Revision 5
Source Link
Vogel612
  • 25.3k
  • 7
  • 58
  • 141

I wrote some program in C language. I have to calculate sum of squares. My book suggested me to use loops but I chose more efficient way by using formula:

$$1^2+2^2+\cdots+n^2=\frac{n\left(n+1\right)\left(2n+1\right)}{6}$$

Original code from listing 5.13:

#include <stdio.h>

int main(void) {
    int ilicznik, sum;suma;

    ilicznik = 0;
    sumsuma = 0;
    while (i++licznik++ < 20)
        sumsuma = sumsuma + i;licznik;
    printf("Sum"suma = %d\n", sumsuma);
    return 0;
}

Finally, this is my code:

#include <stdio.h>

int main(void) {
    int range;zakres;
    printf("Enter the range"Podaj ofzakres theprzeprowadzanych calculationobliczen:\n");
    scanf("%i", &range&zakres);
    printf("Sum of"suma squareskwadratow = %d\n", (((rangezakres * (rangezakres + 1)) * (2 * rangezakres + 1)) / 6));
    return 0;
}

What do you think about this solution? Is it more efficient than loops?

I wrote some program in C language. I have to calculate sum of squares. My book suggested me to use loops but I chose more efficient way by using formula:

$$1^2+2^2+\cdots+n^2=\frac{n\left(n+1\right)\left(2n+1\right)}{6}$$

Original code from listing 5.13:

#include <stdio.h>

int main(void) {
    int i, sum;

    i = 0;
    sum = 0;
    while (i++ < 20)
        sum = sum + i;
    printf("Sum = %d\n", sum);
    return 0;
}

Finally, this is my code:

#include <stdio.h>

int main(void) {
    int range;
    printf("Enter the range of the calculation:\n");
    scanf("%i", &range);
    printf("Sum of squares = %d\n", (((range * (range + 1)) * (2 * range + 1)) / 6));
    return 0;
}

What do you think about this solution? Is it more efficient than loops?

I wrote some program in C language. I have to calculate sum of squares. My book suggested me to use loops but I chose more efficient way by using formula:

$$1^2+2^2+\cdots+n^2=\frac{n\left(n+1\right)\left(2n+1\right)}{6}$$

Original code from listing 5.13:

#include <stdio.h>

int main(void) {
    int licznik, suma;

    licznik = 0;
    suma = 0;
    while (licznik++ < 20)
        suma = suma + licznik;
    printf("suma = %d\n", suma);
    return 0;
}

Finally, this is my code:

#include <stdio.h>

int main(void) {
    int zakres;
    printf("Podaj zakres przeprowadzanych obliczen:\n");
    scanf("%i", &zakres);
    printf("suma kwadratow = %d\n", (((zakres * (zakres + 1)) * (2 * zakres + 1)) / 6));
    return 0;
}

What do you think about this solution? Is it more efficient than loops?

I wrote some program in C language. I have to calculate sum of squares. My book suggested me to use loops but I chose more efficient way by using formula:

$$1^2+2^2+\cdots+n^2=\frac{n\left(n+1\right)\left(2n+1\right)}{6}$$

Original code from listing 5.13:

#include <stdio.h>

int main(void) {
    int liczniki, suma;sum;

    liczniki = 0;
    sumasum = 0;
    while (licznik++i++ < 20)
        sumasum = sumasum + licznik;i;
    printf("suma"Sum = %d\n", sumasum);
    return 0;
}

Finally, this is my code:

#include <stdio.h>

int main(void) {
    int zakres;range;
    printf("Podaj"Enter zakresthe przeprowadzanychrange obliczenof the calculation:\n");
    scanf("%i", &zakres&range);
    printf("suma"Sum kwadratowof squares = %d\n", (((zakresrange * (zakresrange + 1)) * (2 * zakresrange + 1)) / 6));
    return 0;
}

What do you think about this solution? Is it more efficient than loops?

I wrote some program in C language. I have to calculate sum of squares. My book suggested me to use loops but I chose more efficient way by using formula:

$$1^2+2^2+\cdots+n^2=\frac{n\left(n+1\right)\left(2n+1\right)}{6}$$

Original code from listing 5.13:

#include <stdio.h>

int main(void) {
    int licznik, suma;

    licznik = 0;
    suma = 0;
    while (licznik++ < 20)
        suma = suma + licznik;
    printf("suma = %d\n", suma);
    return 0;
}

Finally, this is my code:

#include <stdio.h>

int main(void) {
    int zakres;
    printf("Podaj zakres przeprowadzanych obliczen:\n");
    scanf("%i", &zakres);
    printf("suma kwadratow = %d\n", (((zakres * (zakres + 1)) * (2 * zakres + 1)) / 6));
    return 0;
}

What do you think about this solution? Is it more efficient than loops?

I wrote some program in C language. I have to calculate sum of squares. My book suggested me to use loops but I chose more efficient way by using formula:

$$1^2+2^2+\cdots+n^2=\frac{n\left(n+1\right)\left(2n+1\right)}{6}$$

Original code from listing 5.13:

#include <stdio.h>

int main(void) {
    int i, sum;

    i = 0;
    sum = 0;
    while (i++ < 20)
        sum = sum + i;
    printf("Sum = %d\n", sum);
    return 0;
}

Finally, this is my code:

#include <stdio.h>

int main(void) {
    int range;
    printf("Enter the range of the calculation:\n");
    scanf("%i", &range);
    printf("Sum of squares = %d\n", (((range * (range + 1)) * (2 * range + 1)) / 6));
    return 0;
}

What do you think about this solution? Is it more efficient than loops?

added 27 characters in body
Source Link
200_success
  • 144.1k
  • 22
  • 188
  • 473

I wrote some program in C language. I have to calculate sum of squares. My book suggested me to use loops but I chose more efficient way by using formula:

$$1^2+2^2+\cdots+n^2=\frac{n\left(n+1\right)\left(2n+1\right)}{6}$$

Original code from listing 5.13:

#include <stdio.h>

int main(void) {
    int licznik, suma;

    licznik = 0;
    suma = 0;
    while (licznik++ < 20)
        suma = suma + licznik;
    printf("suma = %d\n", suma);
    return 0;
}
#include <stdio.h>

int main(void) {
    int licznik, suma;

    licznik = 0;
    suma = 0;
    while (licznik++ < 20)
        suma = suma + licznik;
    printf("suma = %d\n", suma);
    return 0;
}

Finally, this is my code:

#include <stdio.h>

int main(void) {
    int zakres;
    printf("Podaj zakres przeprowadzanych obliczen:\n");
    scanf("%i", &zakres);
    printf("suma kwadratow = %d\n", (((zakres * (zakres + 1)) * (2 * zakres + 1)) / 6));
    return 0;
}

What do you think about this solution? Is it more efficient than loops?

I wrote some program in C language. I have to calculate sum of squares. My book suggested me to use loops but I chose more efficient way by using formula:

$$1^2+2^2+\cdots+n^2=\frac{n\left(n+1\right)\left(2n+1\right)}{6}$$

Original code from listing 5.13:

#include <stdio.h>

int main(void) {
    int licznik, suma;

    licznik = 0;
    suma = 0;
    while (licznik++ < 20)
        suma = suma + licznik;
    printf("suma = %d\n", suma);
    return 0;
}

Finally, this is my code:

#include <stdio.h>

int main(void) {
    int zakres;
    printf("Podaj zakres przeprowadzanych obliczen:\n");
    scanf("%i", &zakres);
    printf("suma kwadratow = %d\n", (((zakres * (zakres + 1)) * (2 * zakres + 1)) / 6));
    return 0;
}

What do you think about this solution? Is it more efficient than loops?

I wrote some program in C language. I have to calculate sum of squares. My book suggested me to use loops but I chose more efficient way by using formula:

$$1^2+2^2+\cdots+n^2=\frac{n\left(n+1\right)\left(2n+1\right)}{6}$$

Original code from listing 5.13:

#include <stdio.h>

int main(void) {
    int licznik, suma;

    licznik = 0;
    suma = 0;
    while (licznik++ < 20)
        suma = suma + licznik;
    printf("suma = %d\n", suma);
    return 0;
}

Finally, this is my code:

#include <stdio.h>

int main(void) {
    int zakres;
    printf("Podaj zakres przeprowadzanych obliczen:\n");
    scanf("%i", &zakres);
    printf("suma kwadratow = %d\n", (((zakres * (zakres + 1)) * (2 * zakres + 1)) / 6));
    return 0;
}

What do you think about this solution? Is it more efficient than loops?

deleted 22 characters in body
Source Link
user120313
user120313
Loading
added 8 characters in body
Source Link
user120313
user120313
Loading
Source Link
user120313
user120313
Loading