Python程式計算n+nm+nmm……+n(m次).


這裡n是一個給定的正數,m是級數執行的次數。我們的任務是計算這個級數。

演算法

Step 1: Input n, m;
Step 2: Converting the number to string.
Step 3: Initializing result as number and string.
Step 4: Adding remaining terms.
Step 5: Concatenating the string making n, nn, nnn...
Step 6: Before adding converting back to integer.
Step 7: return sum.

示例程式碼

# Python program to sum the series
def sumofseries(n, m):
   str1 = str(n)
   sum1 = n
   sumofstr1 = str(n)
   for i in range(1, m):
   sumofstr1 = sumofstr1 + str1
   sum1 = sum1 + int(sumofstr1)
   return sum1
   # Driver Code
   n = int(input("Enter the value of n"))
   m = int(input("Enter the value of m"))
sumofno = sumofseries(n, m)
print("SUM OF SERIES ::>",sumofno)

輸出

Enter the value of n3
Enter the value of m5
SUM OF SERIES ::> 37035

更新於:2020年6月23日

443 次瀏覽

開啟你的職業生涯

完成課程獲得認證

開始學習
廣告
© . All rights reserved.