- Trending Categories
- Data Structure
- Operating System
- C Programming
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Calculate n + nn + nnn + ? + n(m times) in Python
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.
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.
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)
Running the above code gives us the following result:
Enter the value of n: 2 Enter the value of m: 4 Sum:> 2468
- Calculate n + nn + nnn + … + n(m times) in Python program
- Calculate n + nn + nnn + u + n(m times) in Python Program
- Plus One in Python
- Instant plus() method in Java
- LocalDate plus() method in Java
- LocalDateTime plus() method in Java
- Duration plus() method in Java
- LocalTime plus() method in Java
- Cplus plus vs Java vs Python?
- Plus One Linked List in C++
- C Program to check Plus Perfect Number
- Now call facebook stream to google plus
- What is the Cost plus pricing method?
- What are the specifications of one plus 5?