There are a variety of mathematical series which python can handle gracefully. One such series is a series of repeated digits. Here we take a digit and add it to the next number which has two such digits and again the next number is three such digits and so on. Finally, we calculate the sum of all such numbers in the series.
Approach
We take a digit and convert it to string. Then concatenate two such strings to get the double digit number and keep concatenating to get higher numbers of such digits. Then we implement a recursive function to add all such numbers generated.
Example
def sumofseries(n, m): # Convert the digit to string str_n = str(n) sum_n = n sum_all_str = str(n) for i in range(1, m): # Concatenate all strings sum_all_str = sum_all_str + str_n sum_n = sum_n + int(sum_all_str) return sum_n # Take inputs n = int(input("Enter the value of n: ")) m = int(input("Enter the value of m: ")) sumofno = sumofseries(n, m) print("Sum:>",sumofno)
Output
Running the above code gives us the following result:
Enter the value of n: 2 Enter the value of m: 4 Sum:> 2468