Skip to main content
Code Review

Return to Question

Rollback to Revision 1
Source Link
pacmaninbw
  • 26.1k
  • 13
  • 47
  • 114

Problem description:

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

12 + 22 + ... + 102 = 385

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

(1 + 2 + ... + 10)2 = 552 = 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?

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

 def sum_square_difference(n):
 return (((n**2) * (n + 1)**2) / 4) - (n * (n + 1) * (2*n + 1) / 6) 

Problem description:

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

12 + 22 + ... + 102 = 385

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

(1 + 2 + ... + 10)2 = 552 = 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?

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

 def sum_square_difference(n):
 return (((n**2) * (n + 1)**2) / 4) - (n * (n + 1) * (2*n + 1) / 6) 

Problem description:

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

12 + 22 + ... + 102 = 385

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

(1 + 2 + ... + 10)2 = 552 = 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?

added 206 characters in body; edited tags
Source Link

Problem description:

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

12 + 22 + ... + 102 = 385

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

(1 + 2 + ... + 10)2 = 552 = 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?

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

 def sum_square_difference(n):
 return (((n**2) * (n + 1)**2) / 4) - (n * (n + 1) * (2*n + 1) / 6) 

Problem description:

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

12 + 22 + ... + 102 = 385

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

(1 + 2 + ... + 10)2 = 552 = 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?

Problem description:

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

12 + 22 + ... + 102 = 385

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

(1 + 2 + ... + 10)2 = 552 = 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?

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

 def sum_square_difference(n):
 return (((n**2) * (n + 1)**2) / 4) - (n * (n + 1) * (2*n + 1) / 6) 
Source Link

Sum square difference

Problem description:

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

12 + 22 + ... + 102 = 385

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

(1 + 2 + ... + 10)2 = 552 = 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?

lang-py

AltStyle によって変換されたページ (->オリジナル) /