In this tutorial, we are going to write code to find the sum of the series n + nn + nnn + ... + n (m times). We can do it very easily in Python. Let's see some examples.
Input: n = 1 m = 5 Series: 1 + 11 + 111 + 1111 + 11111 Output: 12345
Follow the below steps to solve the problem.
1. Initialise the n and m. 2. Initialise total to 0. 3. Make the copy of n to generate next number in the series. 4. Iterate the loop m times. 4.1. Add n to the total. 4.2. Update the n with n * 10 + copy_n. 5. Print the total.
See the code below.
# initializing n and m n = 1 m = 5 # initializing total to 0 total = 0 # making the copy of n to get next number in the series copy_n = n # iterating through the loop for i in range(m): # adding n to total total += n # updating n to get next number in the serias n = n * 10 + copy_n # printing the total print(total)
If you run the above code, you will get the following results.
If you have any doubts in the article, mention them in the comment section.