Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> การเขียนโปรแกรม C

ผลรวม n จำนวนแรกของอนุกรม 5, 12, 23, 38, ... ในภาษา C

อนุกรม 5, 12, 23, 38, ... เป็นอนุกรมกำลังสอง (Quadratic Series) เพราะความต่างระหว่างพจน์อยู่ในอนุกรมเลขคณิต

สูตรผลรวม

Sum = 2[n(n+1)(2n+1)/6] + n(n+1)/2 + 2n

ตัวอย่าง

n = 3 → Sum = 40

n = 6 → Sum = 215

โค้ดตัวอย่าง

#include <stdio.h>

int main() {
    int n = 6;
    int sum = (2 * (n * (n + 1) * (2 * n + 1) / 6) + (n * (n + 1) / 2) + (2 * n));
    printf("the sum of series till %d is %d", n, sum);
    return 0;
}

ผลลัพธ์

the sum of series till 6 is 215