Skip to content

Instantly share code, notes, and snippets.

@HarryRybacki-zz
Created July 5, 2013 00:31
Show Gist options
  • Save HarryRybacki-zz/5930957 to your computer and use it in GitHub Desktop.
Save HarryRybacki-zz/5930957 to your computer and use it in GitHub Desktop.
Sample Project Euler solution with added notes.
{
"metadata": {
"name": "Project Euler -- Problem 1"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "heading",
"level": 3,
"metadata": {},
"source": [
"Project Euler: Problem One"
]
},
{
"cell_type": "heading",
"level": 4,
"metadata": {},
"source": [
"Problem Description:"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"*\"If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.\n",
"Find the sum of all the multiples of 3 or 5 below 1000.\"*\n",
"\n",
"Before we start to dissect the problem, let's make sure everyone understands the language being used.\n",
"\n",
"**Natural numbers:**\n",
"What exactly are the *natural numbers?* First of all they are *whole numbers.* In other words, they have no fractions or decimals. If you were to think of them as fruit, whole numbers would only be entire apples or oranges but never partially eaten ones, hence \"whole.\" More specifically, they are all of *non-negative* whole numbers. This means that they begin with zero and move \"up\" e.g.: 0, 1, 2, 3, ... to infinity. Infinity is another tricky term that we needn't worry about right now.\n",
"\n",
"Note: There has been a lot of debate about if natural numbers start with zero (all non-negative whole numbers) or with one (all positive positive whole numbers). For further reading please reference the the [corresponding wikipage](http://en.wikipedia.org/wiki/Natural_number).\n",
"\n",
"**A is a *multiple* of B:**\n",
"If you think back to middle school arithmetic you will recall that a number is a multiple of another number *if* if when it is divided by the other there is a remainder of *zero.* For example, 10 is a multiple of 5 because 10/5 = 2 with a remainder of 0. However, 10 is not a multiple of 4 because while it also has a quotient of two i.e. 10/4 = 2 it *does not* have a remainder of 0. Going back to our fruit analogy, there is a little bit of that last apple left over. \n",
"\n",
"Now, back to the problem.\n"
]
},
{
"cell_type": "heading",
"level": 4,
"metadata": {},
"source": [
"Solving the problem in Python"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"I am assuming you have a basic understanding of Python syntax as well as a basic understanding of programming basic math operations. If you don't feel free to check out this [relevant Python wikibook](http://en.wikibooks.org/wiki/Python_Programming/Basic_Math)."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Looking back at the problem, what exactly are we trying to do? Well, we are trying to determine the **sum** of all **multiples** of **either** 3 **or** 5 **up to** (but not including!) 1000.\n",
"\n",
"Can you guess which bits we might want to take note of? They may or may not be **bold**.\n",
"\n",
"Thinking about this in a more progrommatic way we could say something like, \"For every whole number from 0 up to 1000, if that number is a multiple of 3 or if that that number is a multiple of 5, add it to the sum.\"\n",
"\n",
"Right away we are able to gather a better sense of what we need to to solve this. To the code.\n",
"\n"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# First, we need to track a sum:\n",
"sum = 0\n",
"\n",
"# Now, what kind of job is iterating made for? Yes, that is a terrible pun. No, I will not remove it. Let's make a for loop:\n",
"for i in range(1000):\n",
"\n",
" # Next, let's set up some conditionals to determine if a number is a multiple of 3\n",
" if i % 3 == 0:\n",
" # if it is, add it to the sum\n",
" sum += i\n",
" # Or if it's a multiple of 5\n",
" elif i % 5 == 0:\n",
" # if it is, add it to the sum\n",
" sum += i\n",
" \n",
"# Finally, lets see what the total is\n",
"print sum"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"233168\n"
]
}
],
"prompt_number": 1
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You may have noted that we can clean this up and make it a bit shorter by compressing our two conditionals into one complex conditional like this:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# comments removed for brevity\n",
"sum = 0\n",
"\n",
"for i in range(1000):\n",
"\n",
" if i % 3 == 0 or i % 5 ==0:\n",
" sum += i\n",
"\n",
"print sum"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"233168\n"
]
}
],
"prompt_number": 2
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"And that's basically it. The code above *is* very simple. However many people have a problem grasping the modulo or \"mod\" function. In most programing languages it is referenced by the percentage **%** symbol. The mod function determines the remainder between two numbers. Let's look at a quick example."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"14 % 4"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "pyout",
"prompt_number": 3,
"text": [
"2"
]
}
],
"prompt_number": 3
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Here, we can see that \"*4* goes into *14*\" three times and has a **remainder** of two. This can be a bit confusing at first glance. However, a sanity check should tell us that 4 goes into 16 four times with a **remainder** of zero, right?"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"16 % 4"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "pyout",
"prompt_number": 4,
"text": [
"0"
]
}
],
"prompt_number": 4
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Right. \n",
"\n",
"Don't feel bad if this doesn't make sense. It doesn't to a lot of people. But, there is a lot more information available on the modulo function [here](http://en.wikipedia.org/wiki/Modulo_operation). I hope you've found this at least mildy interesting / useful. If you have comments or questions please feel free to email me at hrybacki@gmail.com."
]
}
],
"metadata": {}
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment