Skip to main content
Corrected solution
Source Link

Firstly the example you provided form the book does not calculate the sum of n squares. I have corrected the code:

#include <stdio.h>

int main(void) {
    int licznik, suma;
    
    licznik = 0;
    suma = 0;
    while (licznik++ < 10)
        suma = suma + (licznik*licznik);
    printf("suma = %d\n", suma);
    return 0;
}

Now your code. I would create a separate function for the formula called sumSquares. And inside the function you can split the calculation into two variables for the numerator (Polish: licznik) and denominator (Polish: mianownik). I assume the range (Polish: zakres) can be only positive, if so I would use unsigned int and check at the beginning if the entered range (n) is positive.

unsigned int sumSquares(unsigned int n) {
    if (n <= 0) {
        printf("Range must be greater than 0\n");
        return 0;
    }
    
    unsigned int numerator = (n * (n + 1)) * (2 * n + 1);
    unsigned int denominator = 6;
    return numerator / denominator;
}

Now as you see it is more readable and it can be used easier.

#include <stdio.h>

unsigned int sumSquares(int n) {
   unsigned ifint (n <= 0) {
        printf("Range must be greater than 0\n");
        return 0;
    }
    
    unsigned int numerator = (n * (n + 1)) * (2 * n + 1);
    unsigned int denominator = 6;
    return numerator / denominator;
}

int main(void) {
    int range = 20;
    printf("Sum: %d\n"%u\n", sumSquares(range));
    return 0;
}

###Performance I think that not using loops should result in better performance and better efficiency. However, if it comes to speed there is no difference at all. Modern computers are so fast, that seriously it does not matter. I think that we should firstly determine the quality of code by comparing its readability and maintainability. Performance

Firstly the example you provided form the book does not calculate the sum of n squares. I have corrected the code:

#include <stdio.h>

int main(void) {
    int licznik, suma;
    
    licznik = 0;
    suma = 0;
    while (licznik++ < 10)
        suma = suma + (licznik*licznik);
    printf("suma = %d\n", suma);
    return 0;
}

Now your code. I would create a separate function for the formula called sumSquares. And inside the function you can split the calculation into two variables for the numerator (Polish: licznik) and denominator (Polish: mianownik). I assume the range (Polish: zakres) can be only positive, if so I would use unsigned int and check at the beginning if the entered range (n) is positive.

unsigned int sumSquares(int n) {
    if (n <= 0) {
        printf("Range must be greater than 0\n");
        return 0;
    }
    
    unsigned int numerator = (n * (n + 1)) * (2 * n + 1);
    unsigned int denominator = 6;
    return numerator / denominator;
}

Now as you see it is more readable and it can be used easier.

#include <stdio.h>

unsigned int sumSquares(int n) {
    if (n <= 0) {
        printf("Range must be greater than 0\n");
        return 0;
    }
    
    unsigned int numerator = (n * (n + 1)) * (2 * n + 1);
    unsigned int denominator = 6;
    return numerator / denominator;
}

int main(void) {
    int range = 20;
    printf("Sum: %d\n", sumSquares(range));
    return 0;
}

###Performance I think that not using loops should result in better performance and better efficiency. However, if it comes to speed there is no difference at all. Modern computers are so fast, that seriously it does not matter. I think that we should firstly determine the quality of code by comparing its readability and maintainability. Performance

Firstly the example you provided form the book does not calculate the sum of n squares. I have corrected the code:

#include <stdio.h>

int main(void) {
    int licznik, suma;
    
    licznik = 0;
    suma = 0;
    while (licznik++ < 10)
        suma = suma + (licznik*licznik);
    printf("suma = %d\n", suma);
    return 0;
}

Now your code. I would create a separate function for the formula called sumSquares. And inside the function you can split the calculation into two variables for the numerator (Polish: licznik) and denominator (Polish: mianownik). I assume the range (Polish: zakres) can be only positive, if so I would use unsigned int and check at the beginning if the entered range (n) is positive.

unsigned int sumSquares(unsigned int n) {
    unsigned int numerator = (n * (n + 1)) * (2 * n + 1);
    unsigned int denominator = 6;
    return numerator / denominator;
}

Now as you see it is more readable and it can be used easier.

#include <stdio.h>

unsigned int sumSquares(unsigned int n) {
    unsigned int numerator = (n * (n + 1)) * (2 * n + 1);
    unsigned int denominator = 6;
    return numerator / denominator;
}

int main(void) {
    int range = 20;
    printf("Sum: %u\n", sumSquares(range));
    return 0;
}

###Performance I think that not using loops should result in better performance and better efficiency. However, if it comes to speed there is no difference at all. Modern computers are so fast, that seriously it does not matter. I think that we should firstly determine the quality of code by comparing its readability and maintainability. Performance

Fixed grammar
Source Link

Firstly the example you provided form the book does not calculate the sum of n squares. I have corrected the code:

#include <stdio.h>

int main(void) {
    int licznik, suma;
    
    licznik = 0;
    suma = 0;
    while (licznik++ < 10)
        suma = suma + (licznik*licznik);
    printf("suma = %d\n", suma);
    return 0;
}

Now your code. I would create thea separate function for the formula called sumSquares. And inside the function you can split the calculation into two variables for the numerator (Polish: licznik) and denominator (Polish: mianownik). I assume the range (Polish: zakres) can be only positive, if so I would use unsigned int and check at the beginning if the entered range (n) is positive.

unsigned int sumSquares(int n) {
    if (n <= 0) {
        printf("Range must be greater than 0\n");
        return 0;
    }
    
    unsigned int numerator = (n * (n + 1)) * (2 * n + 1);
    unsigned int denominator = 6;
    return numerator / denominator;
}

Now as you see it is more readable and it can be used easier.

#include <stdio.h>

unsigned int sumSquares(int n) {
    if (n <= 0) {
        printf("Range must be greater than 0\n");
        return 0;
    }
    
    unsigned int numerator = (n * (n + 1)) * (2 * n + 1);
    unsigned int denominator = 6;
    return numerator / denominator;
}

int main(void) {
    int range = 20;
    printf("Sum: %d\n", sumSquares(range));
    return 0;
}

###Performance I think that not using loops should result in better performance and better efficiency. However, if it comes to speed there is no difference at all. Modern computers are so fast, that seriously it does not matter. I think that we should firstly determine the quality of code by comparing its readability and maintainability. Performance

Firstly the example you provided form the book does not calculate the sum of n squares. I have corrected the code:

#include <stdio.h>

int main(void) {
    int licznik, suma;
    
    licznik = 0;
    suma = 0;
    while (licznik++ < 10)
        suma = suma + (licznik*licznik);
    printf("suma = %d\n", suma);
    return 0;
}

Now your code. I would create the separate function for the formula called sumSquares. And inside the function you can split the calculation into two variables for the numerator (Polish: licznik) and denominator (Polish: mianownik). I assume the range (Polish: zakres) can be only positive, if so I would use unsigned int and check at the beginning if the entered range (n) is positive.

unsigned int sumSquares(int n) {
    if (n <= 0) {
        printf("Range must be greater than 0\n");
        return 0;
    }
    
    unsigned int numerator = (n * (n + 1)) * (2 * n + 1);
    unsigned int denominator = 6;
    return numerator / denominator;
}

Now as you see it is more readable and it can be used easier.

#include <stdio.h>

unsigned int sumSquares(int n) {
    if (n <= 0) {
        printf("Range must be greater than 0\n");
        return 0;
    }
    
    unsigned int numerator = (n * (n + 1)) * (2 * n + 1);
    unsigned int denominator = 6;
    return numerator / denominator;
}

int main(void) {
    int range = 20;
    printf("Sum: %d\n", sumSquares(range));
    return 0;
}

###Performance I think that not using loops should result in better performance and better efficiency. However, if it comes to speed there is no difference at all. Modern computers are so fast, that seriously it does not matter. I think that we should firstly determine the quality of code by comparing its readability and maintainability. Performance

Firstly the example you provided form the book does not calculate the sum of n squares. I have corrected the code:

#include <stdio.h>

int main(void) {
    int licznik, suma;
    
    licznik = 0;
    suma = 0;
    while (licznik++ < 10)
        suma = suma + (licznik*licznik);
    printf("suma = %d\n", suma);
    return 0;
}

Now your code. I would create a separate function for the formula called sumSquares. And inside the function you can split the calculation into two variables for the numerator (Polish: licznik) and denominator (Polish: mianownik). I assume the range (Polish: zakres) can be only positive, if so I would use unsigned int and check at the beginning if the entered range (n) is positive.

unsigned int sumSquares(int n) {
    if (n <= 0) {
        printf("Range must be greater than 0\n");
        return 0;
    }
    
    unsigned int numerator = (n * (n + 1)) * (2 * n + 1);
    unsigned int denominator = 6;
    return numerator / denominator;
}

Now as you see it is more readable and it can be used easier.

#include <stdio.h>

unsigned int sumSquares(int n) {
    if (n <= 0) {
        printf("Range must be greater than 0\n");
        return 0;
    }
    
    unsigned int numerator = (n * (n + 1)) * (2 * n + 1);
    unsigned int denominator = 6;
    return numerator / denominator;
}

int main(void) {
    int range = 20;
    printf("Sum: %d\n", sumSquares(range));
    return 0;
}

###Performance I think that not using loops should result in better performance and better efficiency. However, if it comes to speed there is no difference at all. Modern computers are so fast, that seriously it does not matter. I think that we should firstly determine the quality of code by comparing its readability and maintainability. Performance

Source Link

Firstly the example you provided form the book does not calculate the sum of n squares. I have corrected the code:

#include <stdio.h>

int main(void) {
    int licznik, suma;
    
    licznik = 0;
    suma = 0;
    while (licznik++ < 10)
        suma = suma + (licznik*licznik);
    printf("suma = %d\n", suma);
    return 0;
}

Now your code. I would create the separate function for the formula called sumSquares. And inside the function you can split the calculation into two variables for the numerator (Polish: licznik) and denominator (Polish: mianownik). I assume the range (Polish: zakres) can be only positive, if so I would use unsigned int and check at the beginning if the entered range (n) is positive.

unsigned int sumSquares(int n) {
    if (n <= 0) {
        printf("Range must be greater than 0\n");
        return 0;
    }
    
    unsigned int numerator = (n * (n + 1)) * (2 * n + 1);
    unsigned int denominator = 6;
    return numerator / denominator;
}

Now as you see it is more readable and it can be used easier.

#include <stdio.h>

unsigned int sumSquares(int n) {
    if (n <= 0) {
        printf("Range must be greater than 0\n");
        return 0;
    }
    
    unsigned int numerator = (n * (n + 1)) * (2 * n + 1);
    unsigned int denominator = 6;
    return numerator / denominator;
}

int main(void) {
    int range = 20;
    printf("Sum: %d\n", sumSquares(range));
    return 0;
}

###Performance I think that not using loops should result in better performance and better efficiency. However, if it comes to speed there is no difference at all. Modern computers are so fast, that seriously it does not matter. I think that we should firstly determine the quality of code by comparing its readability and maintainability. Performance