Home > database >  Arithmetic Sequence equation in Python
Arithmetic Sequence equation in Python

Time:11-13

In today's task i've got to implement the below equation, but im struggling with the final output.

enter image description here

This is what I've made so far:

import math

def f(x, y):
    return ((x   y) / x)**2

summary = 0
sumw = 0

for j in range(1, 5):
    sumw = 0
    for k in range(1, 8):
        sumw  = f(j, k)
    summary  = sumw
print(summary)

OUTPUT:

343.97222222222223

but the final output is unfortunetaly 343.972223

What should I do in this case? Any ideas how to solve this one? Thanks in advance for any kind of help.

CodePudding user response:

In the code, the main problem is that the squared operator has to include the entire sum over the k indexes. This is the correction version:

summary = 2
for j in range(1, 5):
    sumw = 0
    for k in range(1, 8):
        sumw  = (j k)/j
    summary  = sumw**2
print(summary)
# 2130.777777777778

Altenatively, you can also write a one-liner:

summary = 2   sum(sum((j k)/j for k in range(1,8))**2 for j in range(1,5))
  • Related