How to Write a C Program to Compute the Series Using a While Loop
When you need to compute a series in a C program, a while loop can be a powerful tool. In this tutorial, we will guide you through the process of computing the following series:
23 × 53 × 83 × n3(), which is generated by the sequence 2, 5, 8, (3k 2).
What You Will Learn:
Understanding the mathematical series. Constructing a C program using a while loop to compute the series. Using closed-form expressions for large values. Implementing a Python example for both closed-form and loop-based solutions.Understanding the Mathematical Series
The series we need to compute can be described as:
S 23 × 53 × 83 × n3
where each term in the sequence is given by the formula: 3k 2, starting with k 0.
To solve this problem, we can use a while loop in C to iterate through the series until we reach or exceed the value of n.
Constructing the C Program
Here is a sample C program that performs the computation:
#include ltstdio.hgt#include ltmath.hgtint main() { int n; printf("Enter the value of n: "); scanf("%d", n); int sum 0; int term 2; // Start with the first term in the series 2 // Loop until the term exceeds n while (term lt n) { sum pow(term, 3); // Add the cube of the term to the sum term 3 * term 2; // Move to the next term in the series } printf("The computed sum is: %d ", sum); return 0;}
Explanation
Input: The program prompts the user to enter the value of n. Initialization: - n - The value of n is stored. - sum - Initialized to 0 to store the total sum of the series. - term - Initialized to 2, which is the first term of the series. While Loop: - The loop continues as long as term lt n. - Inside the loop, the cube of term is added to sum. - term is then incremented by 3 to get the next term in the series. Output: Finally, the program prints the computed sum.Example Usage
If the user inputs n 10, the output will be the sum of 2^3 and 5^3 as 8^3 exceeds 10, resulting in 8^3 5^3 125 8 133.
Series Summation and Closed-Form Expressions
For a low value n 3kmax - 1, a while or for loop is efficient. Larger values can benefit from summation notation and known closed form sums.
The series can be represented as:
[ S sum_{k1}^{n} (3k - 1)^3 2^3 times 5^3 times 8^3 times ldots times n^3 ]
This can be expanded as:
[ S sum_{k1}^{n} (27k^3 - 27k^2 9k - 1) ]
Distributing the summation sign to make three summations, we get:
[ S 27 sum_{k1}^{n} k^3 - 27 sum_{k1}^{n} k^2 9 sum_{k1}^{n} k - sum_{k1}^{n} 1 ]
The rightmost sum is clearly n. The other summations have known closed-form sums to replace them.
Sum of n, n2, or n3 - Brilliant Math and Science Wiki
[ S frac{9}{4}n(n-1)^2 - 27 cdot frac{n(n-1)(2n-1)}{6} 9 cdot frac{n(n-1)}{2} - n ]
Simplify this to get the closed form sum for S.
[ S frac{9}{4}n(n-1)(3n^2 - n - 2) - n ]
[ S frac{9}{4}n(n-1)(3n^2 - 3n - 4) - n ]
[ S frac{9}{4}n(n-1)(3n^2 - n) - n ]
Python Implementation
This Python example shows both the closed-form and loop-based solutions:
def summation_closed_form(n): return (9 * n * (n - 1) / 4 - 27 * n * (n - 1) * (2 * n - 1) / 6 9 * n * (n - 1) / 2 - n)def sum_loop(n): add_up 0 for k in range(1, n 1): add_up (3 * k - 1) ** 3 return add_upif __name__ '__main__': num 10 # Example n value print(f"Closed form result: {summation_closed_form(num)}") print(f"Loop result: {sum_loop(num)}") assert summation_closed_form(num) sum_loop(num)
We can see that both methods give the same result, confirming the correctness of the closed-form expression.