C Language Program 06: The Summation Formula of the Mathematical Prince Gauss – How to Elegantly Calculate the Sum from 1 to Any Integer

Gauss (Carl Friedrich Gauss) is known as the “Prince of Mathematics”, showing extraordinary mathematical talent from a young age, and his contributions have profoundly influenced various fields such as mathematics, physics, and astronomy..

In 1784, Gauss was only seven years old. On that day, the teacher assigned a task to the students in class to calculate the sum of all integers from 1 to 100. This seemed like a simple task but was actually very tedious, and many students added the numbers one by one using the traditional method.

While everyone was busy with the numbers, Gauss quickly provided the answer:“5050”. Even more surprisingly, Gauss not only gave the result but also explained his thought process.

C Language Program 06: The Summation Formula of the Mathematical Prince Gauss - How to Elegantly Calculate the Sum from 1 to Any Integer

He discovered that if the numbers from 1 to 100 were arranged in a row and then reversed, each pair of numbers would sum to 101. Specifically, the first and last numbers add up to 101, the second and the second last add up to 101, and so on. Gauss found that this method allowed him to quickly arrive at the result—there are 50 pairs, each summing to 101, so the total sum is:

S=50×101=5050

C Language Program 06: The Summation Formula of the Mathematical Prince Gauss - How to Elegantly Calculate the Sum from 1 to Any Integer

This clever calculation method later became known as the “Gauss Summation Formula”, and its mathematical expression is:

S=n(n+1)/2

This formula indicates that to calculate the sum from 1 to any positive integer n, you only need to multiply n by n+1, and then divide by 2.

Although Gauss’s story is widely circulated, it is more like a legend that reflects Gauss’s extraordinary mathematical talent. Given Gauss’s great contributions to mathematics, most people tend to believe that Gauss quickly calculated the result, although the specific details have been embellished and dramatized in the story.

C Language Program 06: The Summation Formula of the Mathematical Prince Gauss - How to Elegantly Calculate the Sum from 1 to Any Integer

In the era of modern computers, we can calculate not just from 1 to 100, but rather the sum from 1 to any integer. When designing this program, the key is to translate the Gauss summation formula into C language, which means determining the core algorithm of the program.

Here is our program code:

#define _CRT_SECURE_NO_WARNINGS#include <stdio.h>#include <stdlib.h>  // Include the header file for system() function
// Calculate the sum from 1 to n using the Gauss summation formula
long long calculateSum(int n) {    return (long long)n * (n + 1) / 2;  // long long type to avoid overflow}
int main() {    int n;
    // Infinite loop until the user chooses to exit    while (1) {        // Clear the screen, use system("cls") on Windows        system("cls");
        printf("Calculate the sum from 1 to any integer\n");        printf("\n");
        // Prompt the user to input an integer        printf("Please enter any integer n (e.g., 100, 12356, etc.):\n");
        // Get user input        scanf("%d", &n);
        // If the user inputs 0, exit the program        if (n == 0) {            break;        }
        // Call the function to calculate the sum        long long sum = calculateSum(n);        printf("\n");        // Output the calculation result        printf("The sum from 1 to %d is: %lld\n", n, sum);
        // Wait for the user to press enter to continue        printf("\nPress any key to continue...\n");        getchar();  // Absorb the newline character caused by the previous scanf        getchar();  // Wait for the user to press enter    }
    return 0;}

The result shows:

C Language Program 06: The Summation Formula of the Mathematical Prince Gauss - How to Elegantly Calculate the Sum from 1 to Any IntegerC Language Program 06: The Summation Formula of the Mathematical Prince Gauss - How to Elegantly Calculate the Sum from 1 to Any IntegerC Language Program 06: The Summation Formula of the Mathematical Prince Gauss - How to Elegantly Calculate the Sum from 1 to Any IntegerC Language Program 06: The Summation Formula of the Mathematical Prince Gauss - How to Elegantly Calculate the Sum from 1 to Any IntegerC Language Program 06: The Summation Formula of the Mathematical Prince Gauss - How to Elegantly Calculate the Sum from 1 to Any IntegerC Language Program 06: The Summation Formula of the Mathematical Prince Gauss - How to Elegantly Calculate the Sum from 1 to Any Integer

Leave a Comment