这个程序是为了找到一个数学级数,我们需要接受n和m的值。n是基数,m是级数运行的次数。
null
例如:
Input : 2 + 22 + 222 + 2222 + 22222 Output : 24690 Input : 12 + 1212 + 121212 Output : 122436
我们首先将数字转换成字符串格式,并定期将它们连接起来。稍后,我们将它们转换回整数,并将它们相加到第m项。如以下程序所示。
# Python program to sum the given series # Returns sum of n + nn + nnn + .... (m times) def Series(n, m): # Converting the number to string str_n = str (n) # Initializing result as number and string sums = n sum_str = str (n) # Adding remaining terms for i in range ( 1 , m): # Concatenating the string making n, nn, nnn... sum_str = sum_str + str_n # Before adding converting back to integer sums = sums + int (sum_str) return sums # Driver Code n = 2 m = 5 total = Series(n, m) print (total) |
输出:
24690
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END