B1 – Project Euler #6

This is a programming exercise. You must write a Python script that solves Project Euler Problem #6. If you do not want to sign up for Project Euler, you may ask the instructor for the correct answer.

Requirements

Project Euler Problem #6 reads as follows:

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

Create a Python script named euler06.py that prints the answer to the above question to the console. You must use variables and at least one loop.

Background

Natural numbers refer to all of the positive integers. For example, the first 10 natural numbers are:

1 2 3 4 5 6 7 8 9 10

To take the square of a number (i.e. take a number to the power 2), you need to use the ** operator in Python. For example, the following code takes the square of the number 3:

3 ** 2

You can also calculate this using the built-in pow() function as follows:

pow(3, 2)

Example

The sum of the squares of the first 10 natural numbers is:

12 + 22 + ... + 102 = 385

The square of the sum of the first 10 natural numbers is:

(1 + 2 + ... + 10)2 = 552 = 3025

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

3025 – 385 = 2640

Double check your program using the answer provided above, then solve for the first 100 natural numbers.

Hints

There are multiple ways to solve this problem. My solution uses variables, the range() function, a for loop, and the ** power operator.

Submission

When complete, show your script to the instructor for credit. If you do not finish by the end of the class, you may email the script to the instructor. Please include the following subject line for your email:

MSAN 501 Day 1 – Euler #6

Make sure you include your full name in the email body.