<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom"><title>TREY STOUT</title><link href="http://treystout.com/" rel="alternate"></link><link href="http://treystout.com//feeds/all.atom.xml" rel="self"></link><id>http://treystout.com/</id><updated>2012-01-18T09:58:00Z</updated><entry><title>Yet Another Story Wherein xargs is the Hero</title><link href="http://treystout.com//yet-another-story-wherein-xargs-is-the-hero.html" rel="alternate"></link><updated>2012-01-18T09:58:00Z</updated><author><name>Trey Stout</name></author><id>tag:treystout.com,2012-01-18://yet-another-story-wherein-xargs-is-the-hero.html/</id><summary type="html">&lt;div class="section" id="the-setting"&gt;
&lt;h2&gt;The Setting&lt;/h2&gt;
&lt;p&gt;So there I was blinking tiredly at my terminal. 2:53AM local time. I had 100
calls to send to a web service, and not much time to do it in. My first impulse
was to script something up, but that felt to heavyweight.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="the-solution-xargs-wget"&gt;
&lt;h2&gt;The Solution (&lt;a class="reference external" href="http://en.wikipedia.org/wiki/Xargs"&gt;xargs&lt;/a&gt; + &lt;a class="reference external" href="http://en.wikipedia.org/wiki/Wget"&gt;wget&lt;/a&gt;)&lt;/h2&gt;
&lt;p&gt;In true unix fashion, the answer was incredibly simple.&lt;/p&gt;
&lt;div class="highlight" style="background: #f0f0f0"&gt;&lt;pre style="line-height: 125%"&gt;&lt;span style="color: #808080"&gt;xargs -i ./file_with_args -I % | wget --user username --password bang! http://somewhere.org/some/call?x=%&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
</summary><category term="programming"></category></entry><entry><title>When to give up your Open Source Project?</title><link href="http://treystout.com//when-to-give-up-your-open-source-project.html" rel="alternate"></link><updated>2010-01-21T16:48:00Z</updated><author><name>Trey Stout</name></author><id>tag:treystout.com,2010-01-21://when-to-give-up-your-open-source-project.html/</id><summary type="html">&lt;img alt="Adoption is the new Pregnant" class="align-right" src="img/adoption.jpg" style="width: 125px;" /&gt;
&lt;p&gt;After recently reading &lt;a class="reference external" href="http://www.codinghorror.com/blog/2009/12/responsible-open-source-code-parenting.html"&gt;Jeff Atwood's plea&lt;/a&gt; to maintainers to not be
douchebags, I have come to the realization that I am a horrible software
parent. I have been writing open source code since 2000, and some has
even enjoyed moderate success. But where I have been dropping the ball
for 10 years is in responsibly letting them go.&lt;/p&gt;
&lt;div class="section" id="exhibit-a"&gt;
&lt;h2&gt;Exhibit A&lt;/h2&gt;
&lt;p&gt;My first project, &lt;a class="reference external" href="http://sourceforge.net/projects/ipm/"&gt;IPM&lt;/a&gt; was just a simple multi-user todo list I
needed for the first company I started. I put it up on sourceforge
in 2000 hoping that someone way better than me at PHP would help me
fix it. But instead I got my first dose of internet popularity. People
actually liked it, and they wanted new features. NO! THEY &lt;em&gt;NEEDED&lt;/em&gt;
NEW FEATURES! It was all very exciting, and the project benefited
from contributions to the code and documentation. It was great for
everyone. Fast forward about a year. IPM is now being used by some groups
at CERN, which makes my nerd gland swell, however I could care less about
adding another task display feature. I had just stopped caring about it.&lt;/p&gt;
&lt;p&gt;I would get emails from concerned users asking when I was going
to fix a bug, or release a new version, and I found myself just
ignoring the emails. Not maliciously, but just out of apathy and a
deluded thought that I would get to them eventually. It turns out that
wasn't very healthy, and the project sits there on several websites
now, neglected and sad. Untouched for almost a decade.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="exhibit-b"&gt;
&lt;h2&gt;Exhibit B&lt;/h2&gt;
&lt;p&gt;My next project to have any success was a PERL monstrosity meant to help
out with the administration of a game server than I ran. The server was
for &lt;a class="reference external" href="http://en.wikipedia.org/wiki/Return_to_Castle_Wolfenstein"&gt;Return to Castle Wolfenstein&lt;/a&gt;. It was a great server with about 30
people hanging out at any given time on any night. Of course being the
internet, some people were fond of showing up and causing trouble. The
native commands for banning someone or kicking them off the server were
clumsy and unreliable. So I wrote a simple interface to the server that
would read what I typed in chat and execute the commands directly via
a socket to the server. It worked very well and eventually ended up on
several hundred other servers. Eventually I created a statistics affiliate
network out of that code base, and as a player community we shared our
abilities with a thompson submachine gun with every player that had been
on the network. This pre-dated stuff like Valve's stats by about 4 years.&lt;/p&gt;
&lt;p&gt;But then... I stopped caring about that too. I ported the bot to Call
of Duty, but quickly lost interest there as well. (See? I didn't even
link to that game's wikipedia page.) And it still sits, waiting like a
good dog for me to come home and love it again. But it's just not going
to happen.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="exhibit-r"&gt;
&lt;h2&gt;Exhibit R&lt;/h2&gt;
&lt;p&gt;I skipped a whole bunch of other projects over the last decade that
you don't care about. This brings us to my current project: &lt;a class="reference external" href="http://code.google.com/p/dwarftherapist/"&gt;Dwarf
Therapist&lt;/a&gt;. I hacked on it for most of the end of last year, and it
has come a long way. But the telltale signs of not caring are showing
their ugly heads.&lt;/p&gt;
&lt;p&gt;But this time, I'm still engaged enough to care about its fate. There
are other contributors who would willingly take it over. I must decide
how and when to do it. And what role I want in the future. I know I
can't bear to see another of my projects just slowly decay. How do other
open-source authors handle this stage of a project?&lt;/p&gt;
&lt;/div&gt;
</summary><category term="question"></category><category term="code"></category></entry><entry><title>Project Euler: Problem One (Constant Time)</title><link href="http://treystout.com//project-euler-problem-one-constant-time.html" rel="alternate"></link><updated>2010-01-06T11:15:00Z</updated><author><name>Trey Stout</name></author><id>tag:treystout.com,2010-01-06://project-euler-problem-one-constant-time.html/</id><summary type="html">&lt;div class="contents topic" id="article-contents"&gt;
&lt;p class="topic-title first"&gt;Article Contents&lt;/p&gt;
&lt;ul class="simple"&gt;
&lt;li&gt;&lt;a class="reference internal" href="#recap" id="id1"&gt;Recap&lt;/a&gt;&lt;ul&gt;
&lt;li&gt;&lt;a class="reference internal" href="#working-smarter" id="id2"&gt;Working Smarter&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;a class="reference internal" href="#functions" id="id3"&gt;Functions&lt;/a&gt;&lt;ul&gt;
&lt;li&gt;&lt;a class="reference internal" href="#solving-our-problem-using-functions" id="id4"&gt;Solving our Problem Using Functions&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;div class="section" id="recap"&gt;
&lt;h2&gt;&lt;a class="toc-backref" href="#id1"&gt;Recap&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;OK, so at this point we've solved &lt;a class="reference external" href="./project-euler-problem-one.html"&gt;problem one&lt;/a&gt; (YAY!) but we've
done it in a naive way. We used what's called a brute force approach
to solve. And we came up with a &lt;a class="reference external" href="http://en.wikipedia.org/wiki/Time_complexity#Linear_time"&gt;linear time&lt;/a&gt; algorithm. Meaning as we
make our target number bigger (10,000,000 for example instead of 1,000)
the time taken to find the solution also increases linearly.&lt;/p&gt;
&lt;p&gt;What if we actually think for a second about what it means to find the
sum of these multiples. Is there a relationship between these numbers
we can exploit to arrive at a more efficient algorithm?&lt;/p&gt;
&lt;div class="section" id="working-smarter"&gt;
&lt;h3&gt;&lt;a class="toc-backref" href="#id2"&gt;Working Smarter&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;Let's step back a bit and remember our good friend &lt;a class="reference external" href="http://en.wikipedia.org/wiki/Euler"&gt;Euler&lt;/a&gt;. He showed
us that we can find the sum of a sequence of integers by simple
induction. The sum of consecutive integers up to &lt;strong&gt;n&lt;/strong&gt; can be
found with &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;n*(n+1)/2&lt;/span&gt;&lt;/tt&gt;. Let's try this out in our lovely
IDLE shell...&lt;/p&gt;
&lt;div class="highlight" style="background: #f0f0f0"&gt;&lt;pre style="line-height: 125%"&gt;&lt;span style="color: #c65d09; font-weight: bold"&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span style="color: #40a070"&gt;1&lt;/span&gt;&lt;span style="color: #666666"&gt;+&lt;/span&gt;&lt;span style="color: #40a070"&gt;2&lt;/span&gt;&lt;span style="color: #666666"&gt;+&lt;/span&gt;&lt;span style="color: #40a070"&gt;3&lt;/span&gt;&lt;span style="color: #666666"&gt;+&lt;/span&gt;&lt;span style="color: #40a070"&gt;4&lt;/span&gt;&lt;span style="color: #666666"&gt;+&lt;/span&gt;&lt;span style="color: #40a070"&gt;5&lt;/span&gt;
&lt;span style="color: #808080"&gt;15&lt;/span&gt;
&lt;span style="color: #c65d09; font-weight: bold"&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span style="color: #40a070"&gt;5&lt;/span&gt; &lt;span style="color: #666666"&gt;*&lt;/span&gt; (&lt;span style="color: #40a070"&gt;5&lt;/span&gt; &lt;span style="color: #666666"&gt;+&lt;/span&gt; &lt;span style="color: #40a070"&gt;1&lt;/span&gt;) &lt;span style="color: #666666"&gt;//&lt;/span&gt; &lt;span style="color: #40a070"&gt;2&lt;/span&gt;
&lt;span style="color: #808080"&gt;15&lt;/span&gt;
&lt;span style="color: #c65d09; font-weight: bold"&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span style="color: #007020"&gt;sum&lt;/span&gt;(&lt;span style="color: #007020"&gt;range&lt;/span&gt;(&lt;span style="color: #40a070"&gt;6&lt;/span&gt;)) &lt;span style="color: #60a0b0; font-style: italic"&gt;# remember range stops at n-1&lt;/span&gt;
&lt;span style="color: #808080"&gt;15&lt;/span&gt;
&lt;span style="color: #c65d09; font-weight: bold"&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span style="color: #60a0b0; font-style: italic"&gt;# WHOA! It works! How about 10?&lt;/span&gt;
&lt;span style="color: #c65d09; font-weight: bold"&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span style="color: #40a070"&gt;1&lt;/span&gt;&lt;span style="color: #666666"&gt;+&lt;/span&gt;&lt;span style="color: #40a070"&gt;2&lt;/span&gt;&lt;span style="color: #666666"&gt;+&lt;/span&gt;&lt;span style="color: #40a070"&gt;3&lt;/span&gt;&lt;span style="color: #666666"&gt;+&lt;/span&gt;&lt;span style="color: #40a070"&gt;4&lt;/span&gt;&lt;span style="color: #666666"&gt;+&lt;/span&gt;&lt;span style="color: #40a070"&gt;5&lt;/span&gt;&lt;span style="color: #666666"&gt;+&lt;/span&gt;&lt;span style="color: #40a070"&gt;6&lt;/span&gt;&lt;span style="color: #666666"&gt;+&lt;/span&gt;&lt;span style="color: #40a070"&gt;7&lt;/span&gt;&lt;span style="color: #666666"&gt;+&lt;/span&gt;&lt;span style="color: #40a070"&gt;8&lt;/span&gt;&lt;span style="color: #666666"&gt;+&lt;/span&gt;&lt;span style="color: #40a070"&gt;9&lt;/span&gt;&lt;span style="color: #666666"&gt;+&lt;/span&gt;&lt;span style="color: #40a070"&gt;10&lt;/span&gt;
&lt;span style="color: #808080"&gt;55&lt;/span&gt;
&lt;span style="color: #c65d09; font-weight: bold"&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span style="color: #007020"&gt;sum&lt;/span&gt;(&lt;span style="color: #007020"&gt;range&lt;/span&gt;(&lt;span style="color: #40a070"&gt;11&lt;/span&gt;)) &lt;span style="color: #60a0b0; font-style: italic"&gt;# remember range stops at n-1&lt;/span&gt;
&lt;span style="color: #808080"&gt;55&lt;/span&gt;
&lt;span style="color: #c65d09; font-weight: bold"&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span style="color: #40a070"&gt;10&lt;/span&gt; &lt;span style="color: #666666"&gt;*&lt;/span&gt; (&lt;span style="color: #40a070"&gt;10&lt;/span&gt; &lt;span style="color: #666666"&gt;+&lt;/span&gt; &lt;span style="color: #40a070"&gt;1&lt;/span&gt;) &lt;span style="color: #666666"&gt;//&lt;/span&gt; &lt;span style="color: #40a070"&gt;2&lt;/span&gt;
&lt;span style="color: #808080"&gt;55&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Is there some way we can use this with factors? Well, how many factors of
3 are there in 1000? Well 1000 doesn't divide by 3, so let's go lower to
999. Hey look! 999 / 3 = 333. So there are 333 factors of 3 in between
1 and 1000. Could we sum a sequence of those numbers somehow? You bet.&lt;/p&gt;
&lt;div class="highlight" style="background: #f0f0f0"&gt;&lt;pre style="line-height: 125%"&gt;&lt;span style="color: #c65d09; font-weight: bold"&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span style="color: #40a070"&gt;333&lt;/span&gt; &lt;span style="color: #666666"&gt;*&lt;/span&gt; (&lt;span style="color: #40a070"&gt;333&lt;/span&gt; &lt;span style="color: #666666"&gt;+&lt;/span&gt; &lt;span style="color: #40a070"&gt;1&lt;/span&gt;) &lt;span style="color: #666666"&gt;//&lt;/span&gt; &lt;span style="color: #40a070"&gt;2&lt;/span&gt;
&lt;span style="color: #808080"&gt;55611&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;But wait a second, isn't that just the sum of integers from 1 to
333? Yep. But thanks to the rules of multiplication it works to just
multiply that sum by the factor we were originally looking for.&lt;/p&gt;
&lt;div class="highlight" style="background: #f0f0f0"&gt;&lt;pre style="line-height: 125%"&gt;&lt;span style="color: #c65d09; font-weight: bold"&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;(&lt;span style="color: #40a070"&gt;333&lt;/span&gt; &lt;span style="color: #666666"&gt;*&lt;/span&gt; (&lt;span style="color: #40a070"&gt;333&lt;/span&gt; &lt;span style="color: #666666"&gt;+&lt;/span&gt; &lt;span style="color: #40a070"&gt;1&lt;/span&gt;) &lt;span style="color: #666666"&gt;//&lt;/span&gt; &lt;span style="color: #40a070"&gt;2&lt;/span&gt;) &lt;span style="color: #666666"&gt;*&lt;/span&gt; &lt;span style="color: #40a070"&gt;3&lt;/span&gt;
&lt;span style="color: #808080"&gt;166833&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;So now we have a reliable way to find the sum of a range, or
the sum of a range of factors. But I don't want to type all
of that again for a different factor. Can't I use my awesome
variable friends? Of course, and we can go one better than that,
by introducing the next epic element of programming. Our friends...&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class="section" id="functions"&gt;
&lt;h2&gt;&lt;a class="toc-backref" href="#id3"&gt;Functions&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;A function is a block of code that has been abstracted enough to work
on various variables. Almost no programming is done without the use of
functions (also known as &amp;quot;subroutines&amp;quot; or &amp;quot;methods&amp;quot; in other contexts)
In python you define a function with the &lt;tt class="docutils literal"&gt;def&lt;/tt&gt; keyword. This instructs
python to define the function for later use, but not run the code yet. The
&lt;tt class="docutils literal"&gt;return&lt;/tt&gt; keyword is the output of the function. You can save the value
of a function's return by assigning a variable to it. Here's a simple
function that squares numbers that are sent to it.&lt;/p&gt;
&lt;div class="note"&gt;
&lt;p class="first admonition-title"&gt;Note&lt;/p&gt;
&lt;p class="last"&gt;entering functions in IDLE is a little weird, just double check your
indentation and press enter on the blank line&lt;/p&gt;
&lt;/div&gt;
&lt;div class="highlight" style="background: #f0f0f0"&gt;&lt;pre style="line-height: 125%"&gt;&lt;span style="background-color: #f0f0f0; padding: 0 5px 0 5px"&gt; 1&lt;/span&gt; &lt;span style="color: #c65d09; font-weight: bold"&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span style="color: #007020; font-weight: bold"&gt;def&lt;/span&gt; &lt;span style="color: #06287e"&gt;square&lt;/span&gt;(num):
&lt;span style="background-color: #f0f0f0; padding: 0 5px 0 5px"&gt; 2&lt;/span&gt; &lt;span style="background-color: #ffffcc"&gt;&lt;span style="color: #808080"&gt;    return num * num&lt;/span&gt;
&lt;/span&gt;&lt;span style="background-color: #f0f0f0; padding: 0 5px 0 5px"&gt; 3&lt;/span&gt; 
&lt;span style="background-color: #f0f0f0; padding: 0 5px 0 5px"&gt; 4&lt;/span&gt; &lt;span style="color: #c65d09; font-weight: bold"&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;square(&lt;span style="color: #40a070"&gt;5&lt;/span&gt;)
&lt;span style="background-color: #f0f0f0; padding: 0 5px 0 5px"&gt; 5&lt;/span&gt; &lt;span style="color: #808080"&gt;25&lt;/span&gt;
&lt;span style="background-color: #f0f0f0; padding: 0 5px 0 5px"&gt; 6&lt;/span&gt; &lt;span style="color: #c65d09; font-weight: bold"&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;square(&lt;span style="color: #40a070"&gt;19&lt;/span&gt;)
&lt;span style="background-color: #f0f0f0; padding: 0 5px 0 5px"&gt; 7&lt;/span&gt; &lt;span style="color: #808080"&gt;361&lt;/span&gt;
&lt;span style="background-color: #f0f0f0; padding: 0 5px 0 5px"&gt; 8&lt;/span&gt; &lt;span style="color: #c65d09; font-weight: bold"&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span style="color: #60a0b0; font-style: italic"&gt;# let&amp;#39;s save those return values&lt;/span&gt;
&lt;span style="background-color: #f0f0f0; padding: 0 5px 0 5px"&gt; 9&lt;/span&gt; &lt;span style="color: #c65d09; font-weight: bold"&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;a &lt;span style="color: #666666"&gt;=&lt;/span&gt; square(&lt;span style="color: #40a070"&gt;4&lt;/span&gt;)
&lt;span style="background-color: #f0f0f0; padding: 0 5px 0 5px"&gt;10&lt;/span&gt; &lt;span style="color: #c65d09; font-weight: bold"&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;b &lt;span style="color: #666666"&gt;=&lt;/span&gt; square(&lt;span style="color: #40a070"&gt;5&lt;/span&gt;)
&lt;span style="background-color: #f0f0f0; padding: 0 5px 0 5px"&gt;11&lt;/span&gt; &lt;span style="color: #c65d09; font-weight: bold"&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;a &lt;span style="color: #666666"&gt;+&lt;/span&gt; b
&lt;span style="background-color: #f0f0f0; padding: 0 5px 0 5px"&gt;12&lt;/span&gt; &lt;span style="color: #808080"&gt;41&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;dl class="docutils"&gt;
&lt;dt&gt;&lt;strong&gt;Line 2&lt;/strong&gt;&lt;/dt&gt;
&lt;dd&gt;this line is indented since it's part of the function's body. Notice
how there is no output after defining the function, it's defined now
and we can call it as often as we like&lt;/dd&gt;
&lt;/dl&gt;
&lt;p&gt;So there you have it. We know that the operation of squaring a number
doesn't depend on what the number is, it's just whatever number we pass
into the function times itself. So this type of thing lends itself well
to being a function.&lt;/p&gt;
&lt;div class="section" id="solving-our-problem-using-functions"&gt;
&lt;h3&gt;&lt;a class="toc-backref" href="#id4"&gt;Solving our Problem Using Functions&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;Let's get back to our summing procedure. Based on our formula, we should
be able to make a function out of this without issue. Let's open up a new
editor from IDLE (File -&amp;gt; New Window) and start writing our new script.&lt;/p&gt;
&lt;table class="highlighttable"&gt;&lt;tr&gt;&lt;td&gt;&lt;div class="linenodiv" style="background-color: #f0f0f0; padding-right: 10px"&gt;&lt;pre style="line-height: 125%"&gt; 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12&lt;/pre&gt;&lt;/div&gt;&lt;/td&gt;&lt;td class="code"&gt;&lt;div class="highlight" style="background: #f0f0f0"&gt;&lt;pre style="line-height: 125%"&gt;&lt;span style="color: #4070a0; font-style: italic"&gt;&amp;quot;&amp;quot;&amp;quot; A constant-time algorithm for problem 1&amp;quot;&amp;quot;&amp;quot;&lt;/span&gt;
&lt;span style="color: #007020; font-weight: bold"&gt;def&lt;/span&gt; &lt;span style="color: #06287e"&gt;sum_to_n&lt;/span&gt;(n):
    &lt;span style="color: #007020; font-weight: bold"&gt;return&lt;/span&gt; n &lt;span style="color: #666666"&gt;*&lt;/span&gt; (n &lt;span style="color: #666666"&gt;+&lt;/span&gt; &lt;span style="color: #40a070"&gt;1&lt;/span&gt;) &lt;span style="color: #666666"&gt;//&lt;/span&gt; &lt;span style="color: #40a070"&gt;2&lt;/span&gt;

&lt;span style="color: #007020; font-weight: bold"&gt;def&lt;/span&gt; &lt;span style="color: #06287e"&gt;sum_of_factors&lt;/span&gt;(factor, n):
    total_factors &lt;span style="color: #666666"&gt;=&lt;/span&gt; n &lt;span style="color: #666666"&gt;//&lt;/span&gt; factor
    &lt;span style="color: #007020; font-weight: bold"&gt;return&lt;/span&gt; sum_to_n(total_factors) &lt;span style="color: #666666"&gt;*&lt;/span&gt; factor

limit &lt;span style="color: #666666"&gt;=&lt;/span&gt; &lt;span style="color: #40a070"&gt;999&lt;/span&gt; &lt;span style="color: #60a0b0; font-style: italic"&gt;# we&amp;#39;re looking for values *under* 1000&lt;/span&gt;
threes &lt;span style="color: #666666"&gt;=&lt;/span&gt; sum_of_factors(&lt;span style="color: #40a070"&gt;3&lt;/span&gt;, limit)
fives &lt;span style="color: #666666"&gt;=&lt;/span&gt; sum_of_factors(&lt;span style="color: #40a070"&gt;5&lt;/span&gt;, limit)
&lt;span style="color: #007020; font-weight: bold"&gt;print&lt;/span&gt;(threes &lt;span style="color: #666666"&gt;+&lt;/span&gt; fives)
&lt;/pre&gt;&lt;/div&gt;
&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;p&gt;So when we save and run this program we get 266333. That's not quite
right? What did we do wrong? Well there are some numbers that have
factors of both 3 &lt;strong&gt;and&lt;/strong&gt; 5. So they must be getting counted
twice. Once when we compute threes and once when we compute fives. How
can we get rid of those? Well what's the lowest common multiple? 15 you
say? Excellent, let's fix it up.&lt;/p&gt;
&lt;table class="highlighttable"&gt;&lt;tr&gt;&lt;td&gt;&lt;div class="linenodiv" style="background-color: #f0f0f0; padding-right: 10px"&gt;&lt;pre style="line-height: 125%"&gt; 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14&lt;/pre&gt;&lt;/div&gt;&lt;/td&gt;&lt;td class="code"&gt;&lt;div class="highlight" style="background: #f0f0f0"&gt;&lt;pre style="line-height: 125%"&gt;&lt;span style="color: #4070a0; font-style: italic"&gt;&amp;quot;&amp;quot;&amp;quot; A constant-time algorithm for problem 1&amp;quot;&amp;quot;&amp;quot;&lt;/span&gt;
&lt;span style="color: #007020; font-weight: bold"&gt;def&lt;/span&gt; &lt;span style="color: #06287e"&gt;sum_to_n&lt;/span&gt;(n):
    &lt;span style="color: #007020; font-weight: bold"&gt;return&lt;/span&gt; n &lt;span style="color: #666666"&gt;*&lt;/span&gt; (n &lt;span style="color: #666666"&gt;+&lt;/span&gt; &lt;span style="color: #40a070"&gt;1&lt;/span&gt;) &lt;span style="color: #666666"&gt;//&lt;/span&gt; &lt;span style="color: #40a070"&gt;2&lt;/span&gt;

&lt;span style="color: #007020; font-weight: bold"&gt;def&lt;/span&gt; &lt;span style="color: #06287e"&gt;sum_of_factors&lt;/span&gt;(factor, n):
    total_factors &lt;span style="color: #666666"&gt;=&lt;/span&gt; n &lt;span style="color: #666666"&gt;//&lt;/span&gt; factor &lt;span style="color: #007020; font-weight: bold"&gt;return&lt;/span&gt; sum_to_n(total_factors) &lt;span style="color: #666666"&gt;*&lt;/span&gt; factor

limit &lt;span style="color: #666666"&gt;=&lt;/span&gt; &lt;span style="color: #40a070"&gt;999&lt;/span&gt;
threes &lt;span style="color: #666666"&gt;=&lt;/span&gt; sum_of_factors(&lt;span style="color: #40a070"&gt;3&lt;/span&gt;, limit)
fives &lt;span style="color: #666666"&gt;=&lt;/span&gt; sum_of_factors(&lt;span style="color: #40a070"&gt;5&lt;/span&gt;, limit)
&lt;span style="color: #4070a0; font-style: italic"&gt;&amp;quot;&amp;quot;&amp;quot;some of the numbers have been counted twice so let&amp;#39;s remove&lt;/span&gt;
&lt;span style="color: #4070a0; font-style: italic"&gt;them from the total&amp;quot;&amp;quot;&amp;quot;&lt;/span&gt;
fifteens &lt;span style="color: #666666"&gt;=&lt;/span&gt; sum_of_factors(&lt;span style="color: #40a070"&gt;15&lt;/span&gt;, limit)
&lt;span style="color: #007020; font-weight: bold"&gt;print&lt;/span&gt;(threes &lt;span style="color: #666666"&gt;+&lt;/span&gt; fives &lt;span style="color: #666666"&gt;-&lt;/span&gt; fifteens)
&lt;/pre&gt;&lt;/div&gt;
&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;p&gt;When we save and run this program we get 233168, which is the correct
answer!  WE WIN!  Now, let's figure out why this is actually a better
solution algorithmically. First we'll time a few solutions. Here's
a big file I wrote that contains all three solutions along with some
timing information...&lt;/p&gt;
&lt;table class="highlighttable"&gt;&lt;tr&gt;&lt;td&gt;&lt;div class="linenodiv" style="background-color: #f0f0f0; padding-right: 10px"&gt;&lt;pre style="line-height: 125%"&gt; 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39&lt;/pre&gt;&lt;/div&gt;&lt;/td&gt;&lt;td class="code"&gt;&lt;div class="highlight" style="background: #f0f0f0"&gt;&lt;pre style="line-height: 125%"&gt;&lt;span style="color: #007020; font-weight: bold"&gt;def&lt;/span&gt; &lt;span style="color: #06287e"&gt;first_solution&lt;/span&gt;(limit):
    &lt;span style="color: #60a0b0; font-style: italic"&gt;# linear time (has to look at every number from 0 to limit)&lt;/span&gt;
    &lt;span style="color: #007020; font-weight: bold"&gt;print&lt;/span&gt;(&lt;span style="color: #007020"&gt;sum&lt;/span&gt;([i &lt;span style="color: #007020; font-weight: bold"&gt;for&lt;/span&gt; i &lt;span style="color: #007020; font-weight: bold"&gt;in&lt;/span&gt; &lt;span style="color: #007020"&gt;range&lt;/span&gt;(&lt;span style="color: #40a070"&gt;1&lt;/span&gt;, limit) &lt;span style="color: #007020; font-weight: bold"&gt;if&lt;/span&gt; i &lt;span style="color: #666666"&gt;%&lt;/span&gt; &lt;span style="color: #40a070"&gt;5&lt;/span&gt; &lt;span style="color: #666666"&gt;==&lt;/span&gt; &lt;span style="color: #40a070"&gt;0&lt;/span&gt; &lt;span style="color: #007020; font-weight: bold"&gt;or&lt;/span&gt; i &lt;span style="color: #666666"&gt;%&lt;/span&gt; &lt;span style="color: #40a070"&gt;3&lt;/span&gt; &lt;span style="color: #666666"&gt;==&lt;/span&gt; &lt;span style="color: #40a070"&gt;0&lt;/span&gt;]))

&lt;span style="color: #007020; font-weight: bold"&gt;def&lt;/span&gt; &lt;span style="color: #06287e"&gt;second_solution&lt;/span&gt;(limit):
    &lt;span style="color: #60a0b0; font-style: italic"&gt;# linear time, but only looks at each factor&lt;/span&gt;
    &lt;span style="color: #007020; font-weight: bold"&gt;print&lt;/span&gt;(&lt;span style="color: #007020"&gt;sum&lt;/span&gt;(
      &lt;span style="color: #007020"&gt;list&lt;/span&gt;(&lt;span style="color: #007020"&gt;range&lt;/span&gt;(&lt;span style="color: #40a070"&gt;0&lt;/span&gt;, limit, &lt;span style="color: #40a070"&gt;3&lt;/span&gt;)) &lt;span style="color: #666666"&gt;+&lt;/span&gt; \
      &lt;span style="color: #007020"&gt;list&lt;/span&gt;(&lt;span style="color: #007020"&gt;range&lt;/span&gt;(&lt;span style="color: #40a070"&gt;0&lt;/span&gt;, limit, &lt;span style="color: #40a070"&gt;5&lt;/span&gt;)) &lt;span style="color: #666666"&gt;+&lt;/span&gt; \
      &lt;span style="color: #007020"&gt;list&lt;/span&gt;(&lt;span style="color: #007020"&gt;range&lt;/span&gt;(&lt;span style="color: #40a070"&gt;0&lt;/span&gt;, &lt;span style="color: #666666"&gt;-&lt;/span&gt;limit, &lt;span style="color: #666666"&gt;-&lt;/span&gt;&lt;span style="color: #40a070"&gt;15&lt;/span&gt;))))

&lt;span style="color: #007020; font-weight: bold"&gt;def&lt;/span&gt; &lt;span style="color: #06287e"&gt;third_solution&lt;/span&gt;(limit):
    &lt;span style="color: #60a0b0; font-style: italic"&gt;# constant time (does not look at any numbers from 0 to limit)&lt;/span&gt;
    limit &lt;span style="color: #666666"&gt;-=&lt;/span&gt; &lt;span style="color: #40a070"&gt;1&lt;/span&gt;
    &lt;span style="color: #007020; font-weight: bold"&gt;def&lt;/span&gt; &lt;span style="color: #06287e"&gt;sum_to_n&lt;/span&gt;(n):
        &lt;span style="color: #007020; font-weight: bold"&gt;return&lt;/span&gt; n &lt;span style="color: #666666"&gt;*&lt;/span&gt; (n &lt;span style="color: #666666"&gt;+&lt;/span&gt; &lt;span style="color: #40a070"&gt;1&lt;/span&gt;) &lt;span style="color: #666666"&gt;//&lt;/span&gt; &lt;span style="color: #40a070"&gt;2&lt;/span&gt;
    &lt;span style="color: #007020; font-weight: bold"&gt;def&lt;/span&gt; &lt;span style="color: #06287e"&gt;sum_of_factors&lt;/span&gt;(factor, n):
        total_factors &lt;span style="color: #666666"&gt;=&lt;/span&gt; n &lt;span style="color: #666666"&gt;//&lt;/span&gt; factor
        &lt;span style="color: #007020; font-weight: bold"&gt;return&lt;/span&gt; sum_to_n(total_factors) &lt;span style="color: #666666"&gt;*&lt;/span&gt; factor
    threes &lt;span style="color: #666666"&gt;=&lt;/span&gt; sum_of_factors(&lt;span style="color: #40a070"&gt;3&lt;/span&gt;, limit)
    fives &lt;span style="color: #666666"&gt;=&lt;/span&gt; sum_of_factors(&lt;span style="color: #40a070"&gt;5&lt;/span&gt;, limit)
    fifteens &lt;span style="color: #666666"&gt;=&lt;/span&gt; sum_of_factors(&lt;span style="color: #40a070"&gt;15&lt;/span&gt;, limit)
    &lt;span style="color: #007020; font-weight: bold"&gt;print&lt;/span&gt;(threes &lt;span style="color: #666666"&gt;+&lt;/span&gt; fives &lt;span style="color: #666666"&gt;-&lt;/span&gt; fifteens)

  &lt;span style="color: #007020; font-weight: bold"&gt;import&lt;/span&gt; &lt;span style="color: #0e84b5; font-weight: bold"&gt;time&lt;/span&gt;
  limits &lt;span style="color: #666666"&gt;=&lt;/span&gt; [&lt;span style="color: #40a070"&gt;1000&lt;/span&gt;, &lt;span style="color: #40a070"&gt;1000000&lt;/span&gt;, &lt;span style="color: #40a070"&gt;100000000&lt;/span&gt;]
  &lt;span style="color: #007020; font-weight: bold"&gt;for&lt;/span&gt; limit &lt;span style="color: #007020; font-weight: bold"&gt;in&lt;/span&gt; limits:
      &lt;span style="color: #007020; font-weight: bold"&gt;print&lt;/span&gt;(&lt;span style="color: #4070a0"&gt;&amp;quot;Finding multiples of 3 or 5 up to&amp;quot;&lt;/span&gt;, limit)
      start &lt;span style="color: #666666"&gt;=&lt;/span&gt; time&lt;span style="color: #666666"&gt;.&lt;/span&gt;time()
      first_solution(limit)
      &lt;span style="color: #007020; font-weight: bold"&gt;print&lt;/span&gt;(&lt;span style="color: #4070a0"&gt;&amp;quot;1st solution: &lt;/span&gt;&lt;span style="color: #70a0d0; font-style: italic"&gt;%0.5f&lt;/span&gt;&lt;span style="color: #4070a0"&gt;&amp;quot;&lt;/span&gt; &lt;span style="color: #666666"&gt;%&lt;/span&gt; (time&lt;span style="color: #666666"&gt;.&lt;/span&gt;time() &lt;span style="color: #666666"&gt;-&lt;/span&gt; start), &lt;span style="color: #4070a0"&gt;&amp;quot;seconds&amp;quot;&lt;/span&gt;)

      start &lt;span style="color: #666666"&gt;=&lt;/span&gt; time&lt;span style="color: #666666"&gt;.&lt;/span&gt;time()
      second_solution(limit)
      &lt;span style="color: #007020; font-weight: bold"&gt;print&lt;/span&gt;(&lt;span style="color: #4070a0"&gt;&amp;quot;2nd solution: &lt;/span&gt;&lt;span style="color: #70a0d0; font-style: italic"&gt;%0.5f&lt;/span&gt;&lt;span style="color: #4070a0"&gt;&amp;quot;&lt;/span&gt; &lt;span style="color: #666666"&gt;%&lt;/span&gt; (time&lt;span style="color: #666666"&gt;.&lt;/span&gt;time() &lt;span style="color: #666666"&gt;-&lt;/span&gt; start), &lt;span style="color: #4070a0"&gt;&amp;quot;seconds&amp;quot;&lt;/span&gt;)

      start &lt;span style="color: #666666"&gt;=&lt;/span&gt; time&lt;span style="color: #666666"&gt;.&lt;/span&gt;time()
      third_solution(limit)
      &lt;span style="color: #007020; font-weight: bold"&gt;print&lt;/span&gt;(&lt;span style="color: #4070a0"&gt;&amp;quot;3rd solution: &lt;/span&gt;&lt;span style="color: #70a0d0; font-style: italic"&gt;%0.5f&lt;/span&gt;&lt;span style="color: #4070a0"&gt;&amp;quot;&lt;/span&gt; &lt;span style="color: #666666"&gt;%&lt;/span&gt; (time&lt;span style="color: #666666"&gt;.&lt;/span&gt;time() &lt;span style="color: #666666"&gt;-&lt;/span&gt; start), &lt;span style="color: #4070a0"&gt;&amp;quot;seconds&amp;quot;&lt;/span&gt;)
&lt;/pre&gt;&lt;/div&gt;
&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;p&gt;which outputs the following...&lt;/p&gt;
&lt;div class="highlight" style="background: #f0f0f0"&gt;&lt;pre style="line-height: 125%"&gt;233168
1st solution: 0.00400 seconds
233168
2nd solution: 0.00500 seconds
233168
&lt;span style="background-color: #ffffcc"&gt;3rd solution: 0.00400 seconds
&lt;/span&gt;Finding multiples of 3 or 5 up to 1000000
233333166668
1st solution: 0.33500 seconds
233333166668
2nd solution: 0.12400 seconds
233333166668
&lt;span style="background-color: #ffffcc"&gt;3rd solution: 0.00400 seconds
&lt;/span&gt;Finding multiples of 3 or 5 up to 100000000
2333333316666668
1st solution: 29.62200 seconds
2333333316666668
2nd solution: 9.80800 seconds
2333333316666668
&lt;span style="background-color: #ffffcc"&gt;3rd solution: 0.00400 seconds
&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Hmm so the second solution is clearly faster than our first, just by
virtue of doing less expensive operations (not doing modulus or any
division). However you can see that as we increase the limit, both the
1st and 2nd algorithms take linearly more time to solve. Only the third
solution is constant time. Notice it takes 4 milliseconds to solve for
any given number. If you keep increasing the limit over 1 billion, the
first two will actually run a 2GB machine out of memory before arriving
at a solution. So not only does the third solution not use much memory
at all, but it always solves in the same amount of time!&lt;/p&gt;
&lt;p&gt;This sort of knowledge is what separates your John Carmacks from
your average Java hacker out of Northwestern Southern Florida State
University. I hope we've all learned something here today.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
</summary><category term="python"></category><category term="project euler"></category><category term="howto"></category><category term="programming"></category></entry><entry><title>Project Euler: Problem One</title><link href="http://treystout.com//project-euler-problem-one.html" rel="alternate"></link><updated>2010-01-04T11:15:00Z</updated><author><name>Trey Stout</name></author><id>tag:treystout.com,2010-01-04://project-euler-problem-one.html/</id><summary type="html">&lt;div class="contents topic" id="article-contents"&gt;
&lt;p class="topic-title first"&gt;Article Contents&lt;/p&gt;
&lt;ul class="simple"&gt;
&lt;li&gt;&lt;a class="reference internal" href="#what-you-need" id="id1"&gt;What You Need&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a class="reference internal" href="#problem-1" id="id2"&gt;Problem 1&lt;/a&gt;&lt;ul&gt;
&lt;li&gt;&lt;a class="reference internal" href="#starting-out" id="id3"&gt;Starting Out&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a class="reference internal" href="#variables-are-your-friends" id="id4"&gt;Variables are your Friends&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a class="reference internal" href="#saving-our-work" id="id5"&gt;Saving our Work&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a class="reference internal" href="#solving-the-problem" id="id6"&gt;Solving the Problem&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;a class="reference internal" href="#if" id="id7"&gt;IF&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a class="reference internal" href="#loops" id="id8"&gt;LOOPS&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a class="reference internal" href="#bonus-points" id="id9"&gt;Bonus Points&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;p&gt;Alright folks. Let's call this programming for non-programmers. I'm always
looking for more folks to get into what I think is one of the coolest
things we can do with our brains. &lt;strong&gt;A program is essentially a
proof&lt;/strong&gt;. Not quite a pure-mathematical proof, but still a proof.&lt;/p&gt;
&lt;p&gt;Who cares? Programming teaches your brain to assess problems in a very
structured way. A lot of the time this leads to a deeper understanding
of your problem, simply because you had to think about all the facets
of it. I could talk for days about how awesome I think programming
is, but let's jump in with a few math problems.&amp;nbsp;Most of what I want
to cover can be found at &lt;a class="reference external" href="http://projecteuler.net"&gt;Project Euler&lt;/a&gt;
but I will guide us through the first few problems there with Python.&lt;/p&gt;
&lt;div class="section" id="what-you-need"&gt;
&lt;h2&gt;&lt;a class="toc-backref" href="#id1"&gt;What You Need&lt;/a&gt;&lt;/h2&gt;
&lt;ul class="simple"&gt;
&lt;li&gt;&lt;a class="reference external" href="http://www.python.org/download"&gt;Python 3.x&lt;/a&gt; for your platform&lt;/li&gt;
&lt;li&gt;about 45 minutes&lt;/li&gt;
&lt;li&gt;your brain&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;div class="section" id="problem-1"&gt;
&lt;h2&gt;&lt;a class="toc-backref" href="#id2"&gt;Problem 1&lt;/a&gt;&lt;/h2&gt;
&lt;blockquote&gt;
&lt;strong&gt;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. Find the sum
of all the multiples of 3 or 5 below 1000.&lt;/strong&gt;&lt;/blockquote&gt;
&lt;p&gt;Let's break down what this problem is all about. It concerns
factoring numbers and summing numbers. If you know how to do addition and
division we're ready to rock. Since the problem gives us a nice example
for a small number (&lt;em&gt;10&lt;/em&gt;) we should write our code to solve that. If the
code works for a small set, it should work for a larger set.&lt;/p&gt;
&lt;div class="section" id="starting-out"&gt;
&lt;h3&gt;&lt;a class="toc-backref" href="#id3"&gt;Starting Out&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;In your start menu (&lt;a class="reference external" href="http://www.python.org/download"&gt;You installed Python,
right?&lt;/a&gt;), you should have an entry
under Python 3.x called &amp;quot;IDLE (Python GUI)&amp;quot;. Click that and you should
see an ugly white window show up with a &lt;strong&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/strong&gt; prompt. This is the
python interactive shell. It's effectively a running python interpreter,
and you can just type code into it. So to start out let's see how we do
arithmetic in Python. We know we're going to need addition and division,
so let's get comfy with those.&lt;/p&gt;
&lt;p&gt;Enter the following into IDLE:&lt;/p&gt;
&lt;div class="highlight" style="background: #f0f0f0"&gt;&lt;pre style="line-height: 125%"&gt;&lt;span style="color: #c65d09; font-weight: bold"&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span style="color: #40a070"&gt;5&lt;/span&gt; &lt;span style="color: #666666"&gt;+&lt;/span&gt; &lt;span style="color: #40a070"&gt;9&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Then press &lt;strong&gt;ENTER&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;The shell should return the result (&lt;em&gt;14&lt;/em&gt;), and hand you another blank
prompt. Well that's all well and good, but my calculator can do that
too. Let's cover division next. Try typing each of the following statements
into your IDLE shell.&lt;/p&gt;
&lt;div class="note"&gt;
&lt;p class="first admonition-title"&gt;Note&lt;/p&gt;
&lt;p class="last"&gt;You do not need to type the &lt;tt class="docutils literal"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/tt&gt;, the shell prints that to show you where to type&lt;/p&gt;
&lt;/div&gt;
&lt;div class="highlight" style="background: #f0f0f0"&gt;&lt;pre style="line-height: 125%"&gt;&lt;span style="color: #c65d09; font-weight: bold"&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span style="color: #40a070"&gt;13&lt;/span&gt; &lt;span style="color: #666666"&gt;//&lt;/span&gt; &lt;span style="color: #40a070"&gt;5&lt;/span&gt;
&lt;span style="color: #808080"&gt;2&lt;/span&gt;
&lt;span style="color: #c65d09; font-weight: bold"&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span style="color: #40a070"&gt;13&lt;/span&gt; &lt;span style="color: #666666"&gt;/&lt;/span&gt; &lt;span style="color: #40a070"&gt;5&lt;/span&gt;
&lt;span style="color: #808080"&gt;2.6&lt;/span&gt;
&lt;span style="color: #c65d09; font-weight: bold"&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span style="color: #40a070"&gt;13&lt;/span&gt; &lt;span style="color: #666666"&gt;%&lt;/span&gt; &lt;span style="color: #40a070"&gt;5&lt;/span&gt;
&lt;span style="color: #808080"&gt;3&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;&lt;strong&gt;WTF?&lt;/strong&gt; What does that mean? The first operation: &lt;tt class="docutils literal"&gt;13 // 5&lt;/tt&gt; told
python to execute &lt;em&gt;&amp;quot;floor division&amp;quot;&lt;/em&gt;, meaning only an integer can
result, and it will be rounded down (hence &amp;quot;floor&amp;quot;). The second operation:
&lt;tt class="docutils literal"&gt;13 / 5&lt;/tt&gt; told python to execute &lt;em&gt;&amp;quot;true division&amp;quot;&lt;/em&gt;, which returns the
best decimal approximation your machine can give for a ratio (2.6 in this
case). The third operation: &lt;tt class="docutils literal"&gt;13 % 5&lt;/tt&gt; told python to execute &lt;em&gt;&amp;quot;modulus&amp;quot;&lt;/em&gt;
or what 4th graders call the &amp;quot;remainder division.&amp;quot; Meaning what number is
left over after 5 evenly divides into 13 as many times as it can.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="variables-are-your-friends"&gt;
&lt;h3&gt;&lt;a class="toc-backref" href="#id4"&gt;Variables are your Friends&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;So now we know how to add numbers, and 3 different ways to do division,
but we're missing one key element. Variables. What if we want to keep
the result of a calculation for later use? We can store it in RAM
and use it as much as we like. Guys with PhDs have been arguing about
variable design for about 70 years, and strong vs weak types, static vs
dynamic types etc... are beyond the scope of this post. So let's just
pretend a variable is like a bucket. A bucket that can hold just about
anything. You can put a &amp;quot;2&amp;quot; in a bucket, or the whole text of War and
Peace. Or an email address, etc... Let's see an example:&lt;/p&gt;
&lt;div class="highlight" style="background: #f0f0f0"&gt;&lt;pre style="line-height: 125%"&gt;&lt;span style="color: #c65d09; font-weight: bold"&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;a &lt;span style="color: #666666"&gt;=&lt;/span&gt; &lt;span style="color: #40a070"&gt;2&lt;/span&gt;
&lt;span style="color: #c65d09; font-weight: bold"&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;b &lt;span style="color: #666666"&gt;=&lt;/span&gt; &lt;span style="color: #40a070"&gt;3&lt;/span&gt;
&lt;span style="color: #c65d09; font-weight: bold"&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;a &lt;span style="color: #666666"&gt;+&lt;/span&gt; b
&lt;span style="color: #808080"&gt;5&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Here we've told python to store the integer &lt;em&gt;2&lt;/em&gt; in the variable &lt;strong&gt;a&lt;/strong&gt;,
the integer &lt;em&gt;3&lt;/em&gt; in &lt;strong&gt;b&lt;/strong&gt; and then add the the values &lt;strong&gt;a&lt;/strong&gt; and &lt;strong&gt;b&lt;/strong&gt;.
Guess what we get? Exactly what you'd expect (&lt;em&gt;5&lt;/em&gt;). There is a lot more
discussion we could have concerning variables, RAM usage, and types,
but we'll skip that for now. We're almost at a spot where we can start
tackling the original problem.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="saving-our-work"&gt;
&lt;h3&gt;&lt;a class="toc-backref" href="#id5"&gt;Saving our Work&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;Up until now we've been using the interactive part of IDLE. However we
haven't written a program yet. If you close IDLE now, you'll lose
everything we've typed so far. So let's start writing a program we
can keep.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;p class="first"&gt;In IDLE, select File -&amp;gt; New Window&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p class="first"&gt;You will see a new blank window show up without the &lt;tt class="docutils literal"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/tt&gt; prompt.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p class="first"&gt;Enter the following into the window&lt;/p&gt;
&lt;table class="highlighttable"&gt;&lt;tr&gt;&lt;td&gt;&lt;div class="linenodiv" style="background-color: #f0f0f0; padding-right: 10px"&gt;&lt;pre style="line-height: 125%"&gt;1
2
3
4&lt;/pre&gt;&lt;/div&gt;&lt;/td&gt;&lt;td class="code"&gt;&lt;div class="highlight" style="background: #f0f0f0"&gt;&lt;pre style="line-height: 125%"&gt;&lt;span style="color: #4070a0; font-style: italic"&gt;&amp;quot;&amp;quot;&amp;quot;If we list all the natural numbers below 10 that are multiples of 3&lt;/span&gt;
&lt;span style="color: #4070a0; font-style: italic"&gt;or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.&lt;/span&gt;

&lt;span style="color: #4070a0; font-style: italic"&gt;Find the sum of all the multiples of 3 or 5 below 1000.&amp;quot;&amp;quot;&amp;quot;&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;p&gt;The triple double quotes around this message are important, you'll notice
IDLE has colored this block of text green. The triple quotes tell python
that this is a multi-line comment, and not to execute it as code. This
is how programmers make notes for themselves inside a program. It's
generally good to write comments like this in your code so you don't
have to keep alt-tabbing to a browser to remember what you were doing,
and for later reference when you find this code 3 years from now and
forgot what it does.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p class="first"&gt;In the menu at the top select File -&amp;gt; Save&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p class="first"&gt;Save the file as &lt;em&gt;problem1.py&lt;/em&gt; on your desktop.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Congratulations, you just wrote your first python program. Well,
not really, since it doesn't do anything yet. So now let's set up some
variables so we can solve the first part of the problem. We know we
want to find multiples of 3 and 5 for all numbers from 1 to 10. So let's
store 10 as a variable.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="solving-the-problem"&gt;
&lt;h3&gt;&lt;a class="toc-backref" href="#id6"&gt;Solving the Problem&lt;/a&gt;&lt;/h3&gt;
&lt;table class="highlighttable"&gt;&lt;tr&gt;&lt;td&gt;&lt;div class="linenodiv" style="background-color: #f0f0f0; padding-right: 10px"&gt;&lt;pre style="line-height: 125%"&gt;1
2
3
4
5
6
7
8&lt;/pre&gt;&lt;/div&gt;&lt;/td&gt;&lt;td class="code"&gt;&lt;div class="highlight" style="background: #f0f0f0"&gt;&lt;pre style="line-height: 125%"&gt;&lt;span style="color: #4070a0; font-style: italic"&gt;&amp;quot;&amp;quot;&amp;quot;If we list all the natural numbers below&lt;/span&gt;
&lt;span style="color: #4070a0; font-style: italic"&gt;10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these&lt;/span&gt;
&lt;span style="color: #4070a0; font-style: italic"&gt;multiples is 23.&lt;/span&gt;

&lt;span style="color: #4070a0; font-style: italic"&gt;Find the sum of all the multiples of 3 or 5 below 1000.&amp;quot;&amp;quot;&amp;quot;&lt;/span&gt;

limit &lt;span style="color: #666666"&gt;=&lt;/span&gt; &lt;span style="color: #40a070"&gt;10&lt;/span&gt;
&lt;span style="color: #007020; font-weight: bold"&gt;print&lt;/span&gt;(&lt;span style="color: #4070a0"&gt;&amp;quot;Starting to find multiples up to&amp;quot;&lt;/span&gt;, limit)
&lt;/pre&gt;&lt;/div&gt;
&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;p&gt;Save the file. Then in the menu select Run -&amp;gt; Run Module or press &lt;tt class="docutils literal"&gt;F5&lt;/tt&gt;.
You will see the results of your program in the other IDLE window. We stored
our upper limit (&lt;em&gt;10&lt;/em&gt;) in the variable called &lt;tt class="docutils literal"&gt;limit&lt;/tt&gt;, we then told python
to print the string &amp;quot;Starting to find multiples up to&amp;quot; and then print
the value of limit after that.&amp;nbsp;When you run this program you should see
&amp;quot;Starting to find multiples up to 10&amp;quot; in the output window.&lt;/p&gt;
&lt;p&gt;How does one find multiples of a number? Well, if you divide number
&lt;strong&gt;a&lt;/strong&gt; by number &lt;strong&gt;b&lt;/strong&gt; and there is no remainder,
that means &lt;strong&gt;b&lt;/strong&gt; is a multiple of &lt;strong&gt;a&lt;/strong&gt;. &lt;em&gt;12&lt;/em&gt; has the multiples &lt;em&gt;1&lt;/em&gt;, &lt;em&gt;2&lt;/em&gt;,
&lt;em&gt;3&lt;/em&gt;, &lt;em&gt;4&lt;/em&gt;, &lt;em&gt;6&lt;/em&gt;, and &lt;em&gt;12&lt;/em&gt;. Because you can divide &lt;em&gt;12&lt;/em&gt; by
any of those numbers without a remainder. The problem states we only
care about numbers that &lt;em&gt;3&lt;/em&gt; or &lt;em&gt;5&lt;/em&gt; divide evenly into. So we don't need to
find every multiple of a given number, we just need to test for reaminderless, or
clean division &lt;em&gt;3&lt;/em&gt; and &lt;em&gt;5&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;Which leads us to the most powerful statement in any programming
language. The one statement that makes computers interesting at all. The
most powerful statement in the world that some may argue makes our brains
quantum machines...&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class="section" id="if"&gt;
&lt;h2&gt;&lt;a class="toc-backref" href="#id7"&gt;IF&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;An &lt;em&gt;if statement&lt;/em&gt; is just that. It's a branching of logic where you can alter
the flow of a program based on variables or expressions. Our brains handle
thousands of these a day, and programming is basically identifying &amp;quot;ifs&amp;quot;
and reacting accordingly. You have to pee, you walk to the bathroom. There
are two doors. If you're a male, you go into the men's room, otherwise
you go into the women's room. Let's start out by finding out if 10 has
3 or 5 as multiples. Your program should now look like this...&lt;/p&gt;
&lt;table class="highlighttable"&gt;&lt;tr&gt;&lt;td&gt;&lt;div class="linenodiv" style="background-color: #f0f0f0; padding-right: 10px"&gt;&lt;pre style="line-height: 125%"&gt; 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12&lt;/pre&gt;&lt;/div&gt;&lt;/td&gt;&lt;td class="code"&gt;&lt;div class="highlight" style="background: #f0f0f0"&gt;&lt;pre style="line-height: 125%"&gt;&lt;span style="color: #4070a0; font-style: italic"&gt;&amp;quot;&amp;quot;&amp;quot;If we list all the natural numbers below&lt;/span&gt;
&lt;span style="color: #4070a0; font-style: italic"&gt;10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these&lt;/span&gt;
&lt;span style="color: #4070a0; font-style: italic"&gt;multiples is 23.&lt;/span&gt;

&lt;span style="color: #4070a0; font-style: italic"&gt;Find the sum of all the multiples of 3 or 5 below 1000.&amp;quot;&amp;quot;&amp;quot;&lt;/span&gt;

limit &lt;span style="color: #666666"&gt;=&lt;/span&gt; &lt;span style="color: #40a070"&gt;10&lt;/span&gt; &lt;span style="color: #007020; font-weight: bold"&gt;print&lt;/span&gt;(&lt;span style="color: #4070a0"&gt;&amp;quot;Starting to find multiples up to&amp;quot;&lt;/span&gt;, limit)

&lt;span style="color: #007020; font-weight: bold"&gt;if&lt;/span&gt; limit &lt;span style="color: #666666"&gt;%&lt;/span&gt; &lt;span style="color: #40a070"&gt;5&lt;/span&gt; &lt;span style="color: #666666"&gt;==&lt;/span&gt; &lt;span style="color: #40a070"&gt;0&lt;/span&gt;:
    &lt;span style="color: #007020; font-weight: bold"&gt;print&lt;/span&gt;(&lt;span style="color: #4070a0"&gt;&amp;quot;5 is a multiple of&amp;quot;&lt;/span&gt;, limit)
&lt;span style="color: #007020; font-weight: bold"&gt;if&lt;/span&gt; limit &lt;span style="color: #666666"&gt;%&lt;/span&gt; &lt;span style="color: #40a070"&gt;3&lt;/span&gt; &lt;span style="color: #666666"&gt;==&lt;/span&gt; &lt;span style="color: #40a070"&gt;0&lt;/span&gt;:
    &lt;span style="color: #007020; font-weight: bold"&gt;print&lt;/span&gt;(&lt;span style="color: #4070a0"&gt;&amp;quot;3 is a multiple of&amp;quot;&lt;/span&gt;, limit)
&lt;/pre&gt;&lt;/div&gt;
&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;p&gt;Save (&lt;tt class="docutils literal"&gt;CTRL+S&lt;/tt&gt;) and run the program (&lt;tt class="docutils literal"&gt;F5&lt;/tt&gt;) and you should see the following as output&lt;/p&gt;
&lt;div class="highlight" style="background: #f0f0f0"&gt;&lt;pre style="line-height: 125%"&gt;Starting to find multiples up to 10 5 is a
multiple of 10
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;But what about the third print statement? Why did the
output not show &amp;quot;3 is a multiple of 10&amp;quot;? Well because it's not. Our if
statements were basically saying if you can modulus 5 into 10 and the
result is 0, then 5 is a multiple of 10, so print that out. We then did
the same thing for 3. But 3 goes into 10, 3 times with a remainder of
1. And 1 does not equal 0, so our print statement didn't execute. This
is the power of if. If you notice in our if statements when were wanted
to test if something equaled another something, we used &lt;tt class="docutils literal"&gt;==&lt;/tt&gt; instead of
&lt;tt class="docutils literal"&gt;=&lt;/tt&gt;. This is very important as &lt;tt class="docutils literal"&gt;=&lt;/tt&gt; is the &amp;quot;assignment operator&amp;quot;. You use
&lt;tt class="docutils literal"&gt;=&lt;/tt&gt; when you want to set something as equal to another thing. When you
want to test if two things are equal you use &lt;tt class="docutils literal"&gt;==&lt;/tt&gt;.&lt;/p&gt;
&lt;p&gt;So this is great and all, but the problem states we should be finding all
the numbers &lt;em&gt;from 1 up to 1000&lt;/em&gt;. Not just 10. Man, it would
suck to copy paste those tests 9 more times, let alone 999 more times. So
how do we tell the machine to test those numbers for us without wearing
out our keyboards? The second most powerful idea in programming:&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="loops"&gt;
&lt;h2&gt;&lt;a class="toc-backref" href="#id8"&gt;LOOPS&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Anytime you need a machine to do something over and over, you use a
loop. There are many different kind of loops, including the famous
infinite loop (which is why our keyboards ever had a &lt;tt class="docutils literal"&gt;BREAK&lt;/tt&gt; key). For
our purposes here we want to loop over the integers from 1 to 10 and
test if each one has 3 or 5 as multiples.&lt;/p&gt;
&lt;table class="highlighttable"&gt;&lt;tr&gt;&lt;td&gt;&lt;div class="linenodiv" style="background-color: #f0f0f0; padding-right: 10px"&gt;&lt;pre style="line-height: 125%"&gt; 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12&lt;/pre&gt;&lt;/div&gt;&lt;/td&gt;&lt;td class="code"&gt;&lt;div class="highlight" style="background: #f0f0f0"&gt;&lt;pre style="line-height: 125%"&gt;&lt;span style="color: #4070a0; font-style: italic"&gt;&amp;quot;&amp;quot;&amp;quot;If we list all the natural numbers below&lt;/span&gt;
&lt;span style="color: #4070a0; font-style: italic"&gt;10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these&lt;/span&gt;
&lt;span style="color: #4070a0; font-style: italic"&gt;multiples is 23.&lt;/span&gt;

&lt;span style="color: #4070a0; font-style: italic"&gt;Find the sum of all the multiples of 3 or 5 below 1000.&amp;quot;&amp;quot;&amp;quot;&lt;/span&gt;

limit &lt;span style="color: #666666"&gt;=&lt;/span&gt; &lt;span style="color: #40a070"&gt;10&lt;/span&gt; &lt;span style="color: #007020; font-weight: bold"&gt;print&lt;/span&gt;(&lt;span style="color: #4070a0"&gt;&amp;quot;Starting to find multiples up to&amp;quot;&lt;/span&gt;, limit)
&lt;span style="color: #007020; font-weight: bold"&gt;for&lt;/span&gt; i &lt;span style="color: #007020; font-weight: bold"&gt;in&lt;/span&gt; &lt;span style="color: #007020"&gt;range&lt;/span&gt;(&lt;span style="color: #40a070"&gt;1&lt;/span&gt;, limit):
    &lt;span style="color: #007020; font-weight: bold"&gt;print&lt;/span&gt;(&lt;span style="color: #4070a0"&gt;&amp;quot;Checking&amp;quot;&lt;/span&gt;, i) &lt;span style="color: #007020; font-weight: bold"&gt;if&lt;/span&gt; i &lt;span style="color: #666666"&gt;%&lt;/span&gt; &lt;span style="color: #40a070"&gt;5&lt;/span&gt; &lt;span style="color: #666666"&gt;==&lt;/span&gt; &lt;span style="color: #40a070"&gt;0&lt;/span&gt;:
        &lt;span style="color: #007020; font-weight: bold"&gt;print&lt;/span&gt;(&lt;span style="color: #4070a0"&gt;&amp;quot;5 is a multiple of&amp;quot;&lt;/span&gt;, i)
    &lt;span style="color: #007020; font-weight: bold"&gt;if&lt;/span&gt; i &lt;span style="color: #666666"&gt;%&lt;/span&gt; &lt;span style="color: #40a070"&gt;3&lt;/span&gt; &lt;span style="color: #666666"&gt;==&lt;/span&gt; &lt;span style="color: #40a070"&gt;0&lt;/span&gt;:
        &lt;span style="color: #007020; font-weight: bold"&gt;print&lt;/span&gt;(&lt;span style="color: #4070a0"&gt;&amp;quot;3 is a multiple of&amp;quot;&lt;/span&gt;, i)
&lt;/pre&gt;&lt;/div&gt;
&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;p&gt;Hey check it out, we made a loop. The line &lt;tt class="docutils literal"&gt;for i in range(1, limit):&lt;/tt&gt;
tells the program to start a loop over the range of
integers from 1, to limit (10). Now notice the code under the &lt;tt class="docutils literal"&gt;for&lt;/tt&gt;
line is indented. This is called the &lt;em&gt;loop body&lt;/em&gt;. The &lt;tt class="docutils literal"&gt;for&lt;/tt&gt; line states
the boundaries of the loop, or how the loop should iterate. The indented
code under it says what to do on each step. On each step of the loop,
python will set the variable &lt;strong&gt;i&lt;/strong&gt; for us, to whatever is next in the
loop. So in plain english, this code says:&lt;/p&gt;
&lt;p&gt;# Do the following 9 times starting with the number 1
# Set &lt;strong&gt;i&lt;/strong&gt; to the current loop step (1)
# Print out which number the loop is currently on (&lt;strong&gt;i&lt;/strong&gt;)
# Check if &lt;strong&gt;i&lt;/strong&gt; modulus &lt;em&gt;5&lt;/em&gt; is zero, if it is, then print out a message
to that effect
# Check if &lt;strong&gt;i&lt;/strong&gt; modulus &lt;em&gt;3&lt;/em&gt; is zero, if it is, then print out a message
to that effect
# Set &lt;strong&gt;i&lt;/strong&gt; to the next value and go back to step 2, unless &lt;strong&gt;i&lt;/strong&gt; was
already set to the last value of the loop&lt;/p&gt;
&lt;p&gt;When you run this program you should see the following...&lt;/p&gt;
&lt;div class="highlight" style="background: #f0f0f0"&gt;&lt;pre style="line-height: 125%"&gt;Starting to find multiples up to 10
Checking 1
Checking 2
Checking 3
3 is a multiple of 3
Checking 4
Checking 5
5 is a multiple of 5
Checking 6
3 is a multiple of 6
Checking 7
Checking 8
Checking 9
3 is a multiple of 9
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Hey it works! We just checked all the numbers from 1 to 10 without having
to copy/paste a bunch of code. It's a little wordy. It was nice seeing
that it checked all the numbers, but we don't need to see that every
time, so let's comment that part out. You can turn any single line into
a comment by putting a &lt;tt class="docutils literal"&gt;#&lt;/tt&gt; at the start of the line. This will make our
output little more concise. Also, the problem stated that we wanted the
sum of these numbers, not just the numbers. So let's change it a bit to
be more in line with the original problem.&lt;/p&gt;
&lt;table class="highlighttable"&gt;&lt;tr&gt;&lt;td&gt;&lt;div class="linenodiv" style="background-color: #f0f0f0; padding-right: 10px"&gt;&lt;pre style="line-height: 125%"&gt; 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16&lt;/pre&gt;&lt;/div&gt;&lt;/td&gt;&lt;td class="code"&gt;&lt;div class="highlight" style="background: #f0f0f0"&gt;&lt;pre style="line-height: 125%"&gt;&lt;span style="color: #4070a0; font-style: italic"&gt;&amp;quot;&amp;quot;&amp;quot;If we list all the natural&lt;/span&gt;
&lt;span style="color: #4070a0; font-style: italic"&gt;numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The&lt;/span&gt;
&lt;span style="color: #4070a0; font-style: italic"&gt;sum of these multiples is 23.&lt;/span&gt;

&lt;span style="color: #4070a0; font-style: italic"&gt;Find the sum of all the multiples of 3 or 5 below 1000.&amp;quot;&amp;quot;&amp;quot;&lt;/span&gt;

limit &lt;span style="color: #666666"&gt;=&lt;/span&gt; &lt;span style="color: #40a070"&gt;10&lt;/span&gt; &lt;span style="color: #007020; font-weight: bold"&gt;print&lt;/span&gt;(&lt;span style="color: #4070a0"&gt;&amp;quot;Starting to find multiples up to&amp;quot;&lt;/span&gt;, limit)

&lt;span style="background-color: #ffffcc"&gt;total &lt;span style="color: #666666"&gt;=&lt;/span&gt; &lt;span style="color: #40a070"&gt;0&lt;/span&gt;
&lt;/span&gt;&lt;span style="color: #007020; font-weight: bold"&gt;for&lt;/span&gt; i &lt;span style="color: #007020; font-weight: bold"&gt;in&lt;/span&gt; &lt;span style="color: #007020"&gt;range&lt;/span&gt;(&lt;span style="color: #40a070"&gt;1&lt;/span&gt;, limit):
    &lt;span style="color: #60a0b0; font-style: italic"&gt;#print(&amp;quot;Checking&amp;quot;, i)&lt;/span&gt;
    &lt;span style="color: #007020; font-weight: bold"&gt;if&lt;/span&gt; i &lt;span style="color: #666666"&gt;%&lt;/span&gt; &lt;span style="color: #40a070"&gt;5&lt;/span&gt; &lt;span style="color: #666666"&gt;==&lt;/span&gt; &lt;span style="color: #40a070"&gt;0&lt;/span&gt; &lt;span style="color: #007020; font-weight: bold"&gt;or&lt;/span&gt; i &lt;span style="color: #666666"&gt;%&lt;/span&gt; &lt;span style="color: #40a070"&gt;3&lt;/span&gt; &lt;span style="color: #666666"&gt;==&lt;/span&gt; &lt;span style="color: #40a070"&gt;0&lt;/span&gt;:
&lt;span style="background-color: #ffffcc"&gt;        total &lt;span style="color: #666666"&gt;+=&lt;/span&gt; i
&lt;/span&gt;        &lt;span style="color: #007020; font-weight: bold"&gt;print&lt;/span&gt;(i, &lt;span style="color: #4070a0"&gt;&amp;quot;has either 3 or 5 as a multiple. Total is now:&amp;quot;&lt;/span&gt;, total)

&lt;span style="color: #007020; font-weight: bold"&gt;print&lt;/span&gt;(&lt;span style="color: #4070a0"&gt;&amp;quot;total: &amp;quot;&lt;/span&gt;, total)
&lt;/pre&gt;&lt;/div&gt;
&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;p&gt;There's actually a lot that changed here.&lt;/p&gt;
&lt;dl class="docutils"&gt;
&lt;dt&gt;&lt;strong&gt;Line 9&lt;/strong&gt;&lt;/dt&gt;
&lt;dd&gt;Before the loop, we set a new variable called &lt;tt class="docutils literal"&gt;total&lt;/tt&gt; we will use this
to track the total of numbers that we find. It starts out at 0 because
before the loop starts we haven't found any numbers.&lt;/dd&gt;
&lt;dt&gt;&lt;strong&gt;Line 11&lt;/strong&gt;&lt;/dt&gt;
&lt;dd&gt;On the first line inside the loop you can see we've
commented out the line that prints &amp;quot;Checking...&amp;quot; to keep our output
from being too wordy. You comment out code in this manner instead of
deleting it in case you want to put it back at some point. All you'd
have to do to get it back is remove the &amp;quot;#&amp;quot;.&lt;/dd&gt;
&lt;dt&gt;&lt;strong&gt;Line 12&lt;/strong&gt;&lt;/dt&gt;
&lt;dd&gt;&lt;p class="first"&gt;We combined the two if statements into a single compound statement. The
if statement now reads&lt;/p&gt;
&lt;blockquote&gt;
&amp;quot;if the current loop value modulus 5 is zero &lt;strong&gt;OR&lt;/strong&gt; the current loop
value modulus 3 is zero...&amp;quot;&lt;/blockquote&gt;
&lt;p class="last"&gt;If a number i satisfies &lt;em&gt;either one&lt;/em&gt; of these statements, then we
execute the code below the if statement. If &lt;strong&gt;i&lt;/strong&gt; matches, then we
add it to the total with &lt;tt class="docutils literal"&gt;+=&lt;/tt&gt;. This means add it to the value that
total already has. If we had used just &lt;tt class="docutils literal"&gt;=&lt;/tt&gt; it wouldn't accumulate a
sum, total would just hold the last value of &lt;strong&gt;i&lt;/strong&gt; that the loop ran.
On each step we also print out the number that matched,
and what the running total is on that step.&lt;/p&gt;
&lt;/dd&gt;
&lt;dt&gt;&lt;strong&gt;Line 16&lt;/strong&gt;&lt;/dt&gt;
&lt;dd&gt;We un-indent from the for loop, meaning Line 16 will run after the for
loop is finished. As the last line of the program, it will print out
the total that we've accumulated.&lt;/dd&gt;
&lt;/dl&gt;
&lt;p&gt;When you run this program it should now look like this...&lt;/p&gt;
&lt;div class="highlight" style="background: #f0f0f0"&gt;&lt;pre style="line-height: 125%"&gt;Starting to find multiples up to 10
3 has either 3 or 5 as a multiple.
Total is now: 3
5 has either 3 or 5 as a multiple.
Total is now: 8
6 has either 3 or 5 as a multiple.
Total is now: 14
9 has either 3 or 5 as a multiple.
Total is now: 23
total: 23
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Wicked! As per our comment at the top of the file, this is the answer
we were looking for! Maybe we can solve the whole problem now! As
you'll recall we're supposed to be doing this for numbers from 1 to
1000. And since we're such awesome programmers this doesn't require many
changes. Since we used a variable for the limit, we can just add a couple
of more zeros to change 10 to 1000! Let's make a couple of changes:&lt;/p&gt;
&lt;table class="highlighttable"&gt;&lt;tr&gt;&lt;td&gt;&lt;div class="linenodiv" style="background-color: #f0f0f0; padding-right: 10px"&gt;&lt;pre style="line-height: 125%"&gt; 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16&lt;/pre&gt;&lt;/div&gt;&lt;/td&gt;&lt;td class="code"&gt;&lt;div class="highlight" style="background: #f0f0f0"&gt;&lt;pre style="line-height: 125%"&gt;&lt;span style="color: #4070a0; font-style: italic"&gt;&amp;quot;&amp;quot;&amp;quot;If we list all the natural&lt;/span&gt;
&lt;span style="color: #4070a0; font-style: italic"&gt;numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The&lt;/span&gt;
&lt;span style="color: #4070a0; font-style: italic"&gt;sum of these multiples is 23.&lt;/span&gt;

&lt;span style="color: #4070a0; font-style: italic"&gt;Find the sum of all the multiples of 3 or 5 below 1000.&amp;quot;&amp;quot;&amp;quot;&lt;/span&gt;

&lt;span style="background-color: #ffffcc"&gt;limit &lt;span style="color: #666666"&gt;=&lt;/span&gt; &lt;span style="color: #40a070"&gt;1000&lt;/span&gt;
&lt;/span&gt;&lt;span style="color: #007020; font-weight: bold"&gt;print&lt;/span&gt;(&lt;span style="color: #4070a0"&gt;&amp;quot;Starting to find multiples up to&amp;quot;&lt;/span&gt;, limit)

total &lt;span style="color: #666666"&gt;=&lt;/span&gt; &lt;span style="color: #40a070"&gt;0&lt;/span&gt; &lt;span style="color: #007020; font-weight: bold"&gt;for&lt;/span&gt; i &lt;span style="color: #007020; font-weight: bold"&gt;in&lt;/span&gt; &lt;span style="color: #007020"&gt;range&lt;/span&gt;(&lt;span style="color: #40a070"&gt;1&lt;/span&gt;, limit):
    &lt;span style="color: #60a0b0; font-style: italic"&gt;#print(&amp;quot;Checking&amp;quot;, i)&lt;/span&gt;
    &lt;span style="color: #007020; font-weight: bold"&gt;if&lt;/span&gt; i &lt;span style="color: #666666"&gt;%&lt;/span&gt; &lt;span style="color: #40a070"&gt;5&lt;/span&gt; &lt;span style="color: #666666"&gt;==&lt;/span&gt; &lt;span style="color: #40a070"&gt;0&lt;/span&gt; &lt;span style="color: #007020; font-weight: bold"&gt;or&lt;/span&gt; i &lt;span style="color: #666666"&gt;%&lt;/span&gt; &lt;span style="color: #40a070"&gt;3&lt;/span&gt; &lt;span style="color: #666666"&gt;==&lt;/span&gt; &lt;span style="color: #40a070"&gt;0&lt;/span&gt;:
        total &lt;span style="color: #666666"&gt;+=&lt;/span&gt; i
&lt;span style="background-color: #ffffcc"&gt;        &lt;span style="color: #60a0b0; font-style: italic"&gt;#print(i, &amp;quot;has either 3 or 5 as a multiple. Total is now:&amp;quot;, total)&lt;/span&gt;
&lt;/span&gt;
&lt;span style="color: #007020; font-weight: bold"&gt;print&lt;/span&gt;(&lt;span style="color: #4070a0"&gt;&amp;quot;total: &amp;quot;&lt;/span&gt;, total)
&lt;/pre&gt;&lt;/div&gt;
&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;dl class="docutils"&gt;
&lt;dt&gt;&lt;strong&gt;Line 6&lt;/strong&gt;&lt;/dt&gt;
&lt;dd&gt;We upped the limit to 1000. If you had just used 10 everywhere limit
is written you'd have to change it in several places, meaning more work,
and more opportunity to miss a spot and introduce a
nasty bug into the program. Variables are your friends.&lt;/dd&gt;
&lt;dt&gt;&lt;strong&gt;Line 14&lt;/strong&gt;&lt;/dt&gt;
&lt;dd&gt;We commented out this line so that we don't get so much
spam in our output.&lt;/dd&gt;
&lt;/dl&gt;
&lt;p&gt;After running this program you should see this in the output window...&lt;/p&gt;
&lt;div class="highlight" style="background: #f0f0f0"&gt;&lt;pre style="line-height: 125%"&gt;Starting to find multiples up to 1000 total:
233168
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Which is the correct answer! It may comfort or frustrate you to know
you can do this whole program in a single line of python. It's a bit
more obfuscated, but it works nonetheless.&lt;/p&gt;
&lt;div class="highlight" style="background: #f0f0f0"&gt;&lt;pre style="line-height: 125%"&gt;&lt;span style="color: #60a0b0; font-style: italic"&gt;# complete program&lt;/span&gt;
&lt;span style="color: #007020; font-weight: bold"&gt;print&lt;/span&gt;(&lt;span style="color: #4070a0"&gt;&amp;quot;total: &amp;quot;&lt;/span&gt;, &lt;span style="color: #007020"&gt;sum&lt;/span&gt;([i &lt;span style="color: #007020; font-weight: bold"&gt;for&lt;/span&gt; i &lt;span style="color: #007020; font-weight: bold"&gt;in&lt;/span&gt; &lt;span style="color: #007020"&gt;range&lt;/span&gt;(&lt;span style="color: #40a070"&gt;1&lt;/span&gt;, &lt;span style="color: #40a070"&gt;1000&lt;/span&gt;) &lt;span style="color: #007020; font-weight: bold"&gt;if&lt;/span&gt; i &lt;span style="color: #666666"&gt;%&lt;/span&gt; &lt;span style="color: #40a070"&gt;5&lt;/span&gt; &lt;span style="color: #666666"&gt;==&lt;/span&gt; &lt;span style="color: #40a070"&gt;0&lt;/span&gt; &lt;span style="color: #007020; font-weight: bold"&gt;or&lt;/span&gt; i &lt;span style="color: #666666"&gt;%&lt;/span&gt; &lt;span style="color: #40a070"&gt;3&lt;/span&gt; &lt;span style="color: #666666"&gt;==&lt;/span&gt; &lt;span style="color: #40a070"&gt;0&lt;/span&gt;]))
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;See if you can figure out what this one line is doing.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="bonus-points"&gt;
&lt;h2&gt;&lt;a class="toc-backref" href="#id9"&gt;Bonus Points&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;How efficient is this algorithm? Did you notice a speed
decrease in changing limit from 10 to 1000? What about 1000000 or
1000000000? How long does it take your machine to compute? Is there
a better way to solve this?&lt;/p&gt;
&lt;p&gt;Thanks for following along. I hope you learned something and had a good time.&lt;/p&gt;
&lt;p&gt;&lt;a class="reference external" href="./project-euler-problem-one-constant-time.html"&gt;In the next section&lt;/a&gt;  I'll cover how brute force approaches
like this aren't always the best idea. And how a little bit of math
knowledge puts you leaps and bounds ahead of most other programmers
out there.&amp;lt;/a&amp;gt;&lt;/p&gt;
&lt;/div&gt;
</summary><category term="python"></category><category term="project euler"></category><category term="howto"></category><category term="programming"></category></entry></feed>
