📅  最后修改于: 2023-12-03 14:50:46.751000             🧑  作者: Mango
This is a programming question from ISRO CS 2013. The problem statement is as follows:
Write a program to find the sum of series 1^1+2^2+3^3+...+n^n
To solve this problem, we need to calculate the sum of the series 1^1+2^2+3^3+...+n^n
. We'll use a loop to iterate over the values of i
from 1 to n
. In each iteration, we'll calculate i^i
and add it to a variable sum
.
Finally, we'll print the value of sum
.
The algorithm can be summarised with these steps:
sum
to 0.i
from 1 to n
do steps 3-4:i^i
and add it to sum
.sum
.Here is the Python 3 code to solve this problem:
n = int(input("Enter a number: "))
sum = 0
for i in range(1, n+1):
sum += i**i
print("The sum of the series is:", sum)
Here is the explanation of the code:
n
from the user.sum
to 0.for
loop to iterate over the values of i
from 1 to n
.i^i
using the **
operator (exponentiation) and add it to the variable sum
.sum
.We have successfully implemented the algorithm to find the sum of the series 1^1+2^2+3^3+...+n^n
. This code can be easily modified to find the sum of other similar series.