Calculate n + nn + nnn +… + n (m times) in Python

Python Methods and Functions

Examples:

 Input: 2 + 22 + 222 + 2222 + 22222 Output: 24690 Input: 12 + 1212 + 121212 Output: 122436 

First, we convert numbers to string format and combine them regularly. Later, we convert them back to an integer and add them before the mth term. as shown in the following program.

# Python program to summarize this series

  
# Returns the sum n + nn + nnn + .... (m times)

def Series (n, m):

  

# Convert number to string

str_n = str (n)

 

# Initialize the result as a number and string

s ums = n

  sum_str = str (n )

 

# Add remaining terms

  for i in range ( 1 , m):

 

# String concatenation makes n, nn, nnn ...

sum_str = sum_str + str_n

  

# Before adding the transform back to integer

sums = sums + int (sum_str)

 

return sums

 
Driver code

n = 2

m = 5

total = Series (n, m)

print (total)

Output:

 24690 




Tutorials