0

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.

2
  • 2
    The entire inner sum needs to be squared, not the individual terms. Move the **2 to the second last line. Commented Nov 13, 2022 at 12:09
  • oh shoot, deffo too early for me, thanks so much! Commented Nov 13, 2022 at 12:11

1 Answer 1

2

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))
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.