用Python计算n+nn+nnn+…+n(m次)

这个程序是为了找到一个数学级数,我们需要接受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
喜欢就支持一下吧
点赞9 分享