Part of the series

Several example codes

~1 min read • Updated Oct 13, 2025

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

Written & researched by Dr. Shahin Siami