How to Calculate Pi in C Language: Methods, Examples, and Accuracy Comparison

1. The Significance and Methods of Calculating Pi in C Language

Pi (π) is defined as the ratio of a circle’s circumference to its diameter and is an essential constant in fields such as mathematics, science, and engineering. Calculating Pi using the C language has great educational value, as it helps in understanding algorithms and learning the fundamentals of numerical computation. In this article, we will explain various methods for calculating Pi in C step-by-step, providing practical knowledge for those interested in numerical computation.

2. Basic Methods of Calculating Pi in C

Calculating Pi Using the Leibniz Series

The Leibniz series calculates Pi using the following infinite series:

[ pi = 4 times left( 1 – frac{1}{3} + frac{1}{5} – frac{1}{7} + cdots right) ]

Implementation Example

Below is a C program that calculates Pi using the Leibniz series. The user specifies the number of iterations, and the program computes an approximation of Pi accordingly.

#include <stdio.h>

int main() {
    int n, i;
    double pi = 0.0;
    int sign = 1;

    printf("Enter the number of iterations: ");
    scanf("%d", &n);

    for (i = 0; i < n; i++) {
        pi += sign * (4.0 / (2 * i + 1));
        sign = -sign;
    }

    printf("Approximation of Pi: %.15fn", pi);
    return 0;
}

Advantages: Simple implementation and easy to understand
Disadvantages: Very slow convergence, making it unsuitable for high-precision calculations

侍エンジニア塾

3. Estimating Pi Using the Monte Carlo Method

The Monte Carlo method estimates Pi by generating random points inside a square, checking whether they fall inside the circle, and calculating Pi based on probability.

Implementation Example

Below is a C program that approximates Pi using the Monte Carlo method.

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main() {
    int n, i;
    int inside_circle = 0;
    double x, y, pi;

    printf("Enter the total number of points: ");
    scanf("%d", &n);

    srand(time(0));

    for (i = 0; i < n; i++) {
        x = (double)rand() / RAND_MAX;
        y = (double)rand() / RAND_MAX;

        if ((x * x + y * y) <= 1) {
            inside_circle++;
        }
    }

    pi = 4.0 * inside_circle / n;
    printf("Approximation of Pi: %.15fn", pi);

    return 0;
}

Advantages: Simple to implement and useful for learning probability concepts
Disadvantages: Slow convergence, not suitable for high-precision Pi calculations

4. Gauss–Legendre Algorithm

The Gauss–Legendre algorithm is an efficient method that calculates Pi to very high precision with only a few iterations.

Implementation Example

#include <stdio.h>
#include <math.h>

int main() {
    double a = 1.0;
    double b = 1.0 / sqrt(2.0);
    double t = 0.25;
    double p = 1.0;
    double pi;
    int n, iterations;

    printf("Enter the number of iterations: ");
    scanf("%d", &iterations);

    for (n = 0; n < iterations; n++) {
        double a_next = (a + b) / 2.0;
        double b_next = sqrt(a * b);
        double t_next = t - p * pow(a - a_next, 2);

        a = a_next;
        b = b_next;
        t = t_next;
        p = 2 * p;
    }

    pi = pow(a + b, 2) / (4 * t);
    printf("Approximation of Pi: %.15fn", pi);

    return 0;
}

Advantages: Very fast convergence and high precision
Disadvantages: More complex implementation, requires knowledge of numerical computation

5. Comparison of Accuracy and Efficiency

MethodAccuracyConvergence SpeedExecution TimeUse Cases
Leibniz SeriesLowSlowLongBasic implementation practice, learning
Machin’s FormulaMedium–HighFastRelatively shortWhen a practical approximation is needed
Monte Carlo MethodMediumSlowDepends on number of pointsProbability/statistics-based simulation
Gauss–Legendre AlgorithmVery HighVery FastShortWhen high precision is required

6. Conclusion

There are many approaches to calculating Pi in C, each differing in convergence speed, precision, and computational cost. Choose the method that best suits your program’s purpose and precision requirements. For high-precision calculations, the Gauss–Legendre algorithm is recommended, while for simpler implementations, the Leibniz series is a good choice. Use different methods depending on your learning goals and application needs.