بخشی از مجموعه

Several example codes

~1 دقیقه مطالعه • بروزرسانی ۲۱ مهر ۱۴۰۴

Program Overview

This Python program reads an integer n and computes the result of the following series:
∑ [(n - k) · k^(n - k)] for k from 1 to n
Each term multiplies a linear coefficient by a power of k.


Python Code:


def compute_series(n: int) -> int:
    total = 0
    for k in range(1, n + 1):
        term = (n - k) * (k ** (n - k))
        total += term
    return total

# Read input from user
n = int(input("Enter value for n: "))
result = compute_series(n)
print(f"Series result is: {result}")

Sample Output (input: n = 4):


Series result is: 78

نوشته و پژوهش شده توسط دکتر شاهین صیامی