0
\$\begingroup\$

Problem description:

The sum of the squares of the first ten natural numbers is,

1² + 2² + … + 10² = 385

The square of the sum of the first ten natural numbers is,

(1 + 2 + … + 10)² = 55² = 3025

Hence the difference between the sum of the squares of the first ten natural numbers and the square of the sum is:

3025 – 385 = 2640.

Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum.

Square difference:

l = sum of the squares of the first n natural numbers

k = sum of n naturals numbers

m = differences between l and k

My Solution

This is my solution for problem 6 of Project Euler using Python:

 def square_difference(n):
 
    l = (n * (n + 1) * (2 * n + 1)) / 6  
     
    k = (n * (n + 1)) / 2   
 
    k = k ** 2   
     
    m = abs(l - k)    
     
    return m

How could my code be improved?

\$\endgroup\$
3
  • 1
    \$\begingroup\$ Note that the sum of the squares will always be less than the square of the sum (so m = k - l); for example see this MSE question \$\endgroup\$
    – Joffan
    Commented May 27, 2022 at 15:42
  • 2
    \$\begingroup\$ Please do not edit the question, especially the code, after an answer has been posted. Changing the question may cause answer invalidation. Everyone needs to be able to see what the reviewer was referring to. What to do after the question has been answered. In this particular case it would be better to add a follow up question with a link back to this question. \$\endgroup\$
    – pacmaninbw
    Commented May 28, 2022 at 12:38
  • \$\begingroup\$ @pacmaninbw How could my code be improved? \$\endgroup\$ Commented May 29, 2022 at 4:59

2 Answers 2

3
\$\begingroup\$

The other answer observes that some one character variable names can be confused. This can partly be avoided by choosing a better font in your editor (although O will always be problematic). Even then, you want to use more descriptive variable names. You went to the trouble to tell us about l, k, and m. Why not just make those names a bit more descriptive? This might also help you notice that k changes between your comments about k and m.

I don't see much point in assigning something to m in order to return m in the next line. Let's skip that step and just return.

As noted in comments, the square of the sum is always greater than the sum of the squares. So we can write the difference that way and leave out the abs.

Empty lines can be useful for creating blocks of thought, but there's not that much going on in your function, so I'd say we don't really need those lines.

To be really pedantic, this is not your solution to problem #6. This is your function used to find the solution to problem #6, but the actual solution would be evaluating this function at 100.

I added a docstring to the function, so that you can call help(square_difference) in other code.

I've removed some unnecessary parentheses.

This gives us:

def square_difference(n):
    ''' Return the difference between the square of the sum of the first n
    natural numbers and the sum of the squares of the first n natural
    numbers.
    '''
    sum_of_squares = n * (n + 1) * (2 * n + 1) / 6  
    sum_of_terms = n * (n + 1) / 2   
    return sum_of_terms**2 - sum_of_squares

print(square_difference(100))

If your function was really causing a bottleneck (it isn't), you could get a bit more performance out of this by using some algebra to find that the result is n*(n+1)*(3*n+2)*(n-1)/12. But if you did that, you'd need a good comment explaining what is happening. As things are, we don't really need any comments.

\$\endgroup\$
1
\$\begingroup\$

There are some one-character variable names one should avoid - for example I, l, O, o.

The problem is that anyone reading the code can confuse them with numbers like 0 or 1

\$\endgroup\$
2
  • 4
    \$\begingroup\$ Even then, longer names are better. Why make us read "l = sum of the squares of the first n natural numbers", when you could have used sum_of_squares instead of l in the first place? \$\endgroup\$
    – Teepeemm
    Commented May 27, 2022 at 22:52
  • 1
    \$\begingroup\$ @Teepeemm I agree with that, but I think you should tell that the OP \$\endgroup\$
    – miracle173
    Commented May 28, 2022 at 18:39

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.