Example of Python method for solving sequence sum

  • 2020-07-21 08:34:57
  • OfStack

An example of Python is presented in this paper. To share for your reference, specific as follows:

Question:

The input

The input data has multiple groups, each consisting of 1 row, consisting of two integers n (n) < 10000) and m (m < 1000) composition, n and m have the meanings described above.

The output

For each set of input data, output the sum of the sequence, 1 row for each test instance, and reserve 2 decimal places for accuracy.

The sample input

81 4
2 2

Sample output

94.73
3.41

Implementation code:


import math
while 1:
  x = raw_input()
  x = list(x.split(" "))
  data = [int(x[0]),]
  nums = int(x[1])
  for i in range(nums-1):
    data.append(math.sqrt(data[-1]))
  print '%.2f' %sum(data)

The above is a bit wordy


import math
while 1:
  x = map(int, raw_input().split(" "))
  data = [int(x[0]),]
  nums = int(x[1])
  for i in range(nums-1):
    data.append(math.sqrt(data[-1]))
  print '%.2f' %sum(data)

For more information about Python, please refer to: Summary of Python Mathematical Operation Skills, Python Data Structure and Algorithm Tutorial, Python Function Skills Summary, Python String Operation Skills Summary, Python Introduction and Advanced Classic Tutorial and Python File and Directory Operation Skills Summary.

I hope this article has helped you with the Python programming.


Related articles: