Calculate n + nn + nnn + … + n(m times) in Python
The program is to find a mathematical series, where we need to accept the value of n and m. n is the base number and m is the number of times till which the series run.
Examples:
Input : 2 + 22 + 222 + 2222 + 22222
Output : 24690
Input : 12 + 1212 + 121212
Output : 122436
We first convert the numbers into string format and concatenate them regularly. Later, we convert them back to integer and add them upto mth term. as shown in the following program.
def Series(n, m):
str_n = str (n)
sums = n
sum_str = str (n)
for i in range ( 1 , m):
sum_str = sum_str + str_n
sums = sums + int (sum_str)
return sums
n = 2
m = 5
total = Series(n, m)
print (total)
|
Output:
24690
Last Updated :
05 Nov, 2019
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...