<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">

<channel>
	<title>BetterExplained</title>
	
	<link>http://betterexplained.com</link>
	<description>Learning shouldn't hurt. Let's share the insights that made difficult ideas click.</description>
	<lastBuildDate>Wed, 10 Mar 2010 16:00:45 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/Betterexplained" /><feedburner:info uri="betterexplained" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><feedburner:emailServiceId>Betterexplained</feedburner:emailServiceId><feedburner:feedburnerHostname>http://feedburner.google.com</feedburner:feedburnerHostname><item>
		<title>Aha! Moments When Learning Git</title>
		<link>http://feedproxy.google.com/~r/Betterexplained/~3/u8znQZCnfJg/</link>
		<comments>http://betterexplained.com/articles/aha-moments-when-learning-git/#comments</comments>
		<pubDate>Wed, 10 Mar 2010 16:00:45 +0000</pubDate>
		<dc:creator>Kalid</dc:creator>
				<category><![CDATA[Guides]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[git]]></category>
		<category><![CDATA[version control]]></category>

		<guid isPermaLink="false">http://betterexplained.com/?p=603</guid>
		<description><![CDATA[Git is a fast, flexible but challenging distributed version control system. Before jumping in:

    Understand regular version control
    Understand distributed version control

Along with a book, tutorial and cheatsheet, here are the insights that helped git click.
There&#8217;s a staging area!
Git has a staging area. Git has a staging area!!!
Yowza, did [...]]]></description>
			<content:encoded><![CDATA[<p>Git is a fast, flexible but challenging distributed version control system. Before jumping in:</p>
<ul>
    <li><a class=" external" title="http://betterexplained.com/articles/a-visual-guide-to-version-control/" href="http://betterexplained.com/articles/a-visual-guide-to-version-control/">Understand regular version control</a></li>
    <li><a class=" external" title="http://betterexplained.com/articles/intro-to-distributed-version-control-illustrated/" href="http://betterexplained.com/articles/intro-to-distributed-version-control-illustrated/">Understand distributed version control</a></li>
</ul>
<p>Along with a <a class=" external" title="http://progit.org/book/" href="http://progit.org/book/">book</a>, <a class=" external" title="http://www.eecs.harvard.edu/~cduan/technical/git/" href="http://www.eecs.harvard.edu/~cduan/technical/git/">tutorial</a> and <a class=" external" title="http://jonas.nitro.dk/git/quick-reference.html" href="http://jonas.nitro.dk/git/quick-reference.html">cheatsheet</a>, here are the insights that helped git click.</p>
<h2>There&#8217;s a staging area!</h2>
<p>Git has a staging area. <strong>Git has a staging area!!!</strong></p>
<p>Yowza, did this ever confuse me. There&#8217;s both a repo (&#8221;object database&#8221;) and a staging area (called &#8220;index&#8221;). Checkins have two steps:</p>
<ul>
    <li><code>git add foo.txt</code>
    <ul>
        <li>Add foo.txt to the index. It&#8217;s not checked in yet!</li>
    </ul>
    </li>
    <li><code>git commit -m "message"</code>
    <ul>
        <li>Put staged files in the repo; they&#8217;re now tracked</li>
        <li>You can &#8220;<code>git add --update"</code> to stage all tracked, modified files</li>
    </ul>
    </li>
</ul>
<p><strong>Why stage?</strong> Git&#8217;s flexible: if a, b and c are changed, you can commit them separately or together.</p>
<p>But now there&#8217;s two undos:</p>
<ul>
    <li><code>git checkout foo.txt</code>
    <ul>
        <li>Undo local changes (like svn revert)</li>
    </ul>
    </li>
    <li><code>git reset HEAD foo.txt</code>
    <ul>
        <li>Remove from staging area (local copy still modified).</li>
    </ul>
    </li>
</ul>
<p>Add and commit, add and commit &#8212; Git has a rhythm.</p>
<h2>Branching is &#8220;Save as&#8230;&#8221;</h2>
<p >Branches are like &#8220;Save as&#8230;&#8221; on a directory. Best of all:</p>
<ul >
    <li >Easily merge changes with the original (changes tracked and never applied twice)</li>
    <li >No wasted space (common files only stored once)</li>
</ul>
<p ><strong>Why branch?</strong> Consider the utility of &#8220;Save as&#8230;&#8221; for regular files: you tinker with multiple possibilities while keeping the original safe. Git enables this for directories, with the power to merge. (In practice, svn is like a single shared drive, where you can only revert to one backup).</p>
<h2>Imagine virtual directories</h2>
<p>I see branches as &#8220;virtual directories&#8221; in the .git folder. While inside a physical directory (c:\project or ~/project), you traverse virtual directories with a checkout.</p>
<ul>
    <li><code>git checkout master</code>
    <ul>
        <li>switch to master branch (&#8221;cd master&#8221;)</li>
    </ul>
    </li>
    <li><code>git branch dev</code>
    <ul>
        <li>create new branch from existing (&#8221;cp * dev&#8221;)</li>
        <li>you still need to &#8220;cd&#8221; with &#8220;git checkout dev&#8221;</li>
    </ul>
    </li>
    <li><code>git merge dev</code>
    <ul>
        <li>(when in master) pull in changes from dev (&#8221;cp dev/* .&#8221;)</li>
    </ul>
    </li>
    <li><code>git branch</code>
    <ul>
        <li>list all branches (&#8221;ls&#8221;)</li>
    </ul>
    </li>
</ul>
<p>My inner dialogue is &#8220;change to dev directory (checkout)&#8230; make changes&#8230; save changes (add/commit)&#8230; change to master directory&#8230; copy in changes from dev (merge)&#8221;.</p>
<p>The physical directory is a scratchpad. Virtual directories are affected by git commands:</p>
<ul>
    <li><code>rm foo.txt</code>
    <ul>
        <li>Remove foo.txt from your sandbox (restored if you checkout the branch again)</li>
    </ul>
    </li>
    <li><code>git rm foo.txt</code>
    <ul>
        <li>Remove foo.txt from current virtual directory</li>
        <li>Gotcha: you need to commit that change!</li>
    </ul>
    </li>
</ul>
<h2>Know the current branch</h2>
<p>Just like seeing your current directory, <strong>put the current branch in your prompt!</strong></p>
<p><img alt="" src="http://betterexplained.com/wp-content/uploads/git/git_prompt.png" /></p>
<p>In my .bash_profile (modified from <a class=" external" href="http://asemanfar.com/Current-Git-Branch-in-Bash-Prompt">here</a>):</p>


<pre>
<code>

parse_git_branch() {
    git branch 2&gt; /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1)/'
}

export PS1="\[\033[00m\]\u@\h\[\033[01;34m\] \W \[\033[31m\]\$(parse_git_branch) \[\033[00m\]$\[\033[00m\] "

</code>
</pre>


<h2>Visualize your branch structure</h2>
<p>Git leaves branch organization to you. Nvie.com has a <a title="http://nvie.com/git-model" class=" external" href="http://nvie.com/git-model">great branch strategy</a>:</p>
<p><img alt="" src="http://betterexplained.com/wp-content/uploads/git/git_branch_strategy.png" /></p>
<ul>
    <li>Have a mainline (master). Mentally it&#8217;s on the far right.</li>
    <li>Create branches (master -&gt; dev) and subbranches (dev -&gt; featureX). The further from master, the crazier.</li>
    <li>Only merge with neighbors (master -&gt; dev -&gt; feature X, or featureX -&gt; dev -&gt; master)</li>
</ul>
<p>Stay sane by choosing a branch layout up front. I have a master tracking a svn project, and dev for my own code. In general, master is clean so I can branch anytime for one-off fixes.</p>
<h2>Understand local vs. remote</h2>
<p>Git has local and remote commands; seeing both confused me (&#8221;When do you checkout vs. pull?&#8221;). Work locally, syncing remotely as needed.</p>
<p><strong>Local data</strong></p>
<ul>
    <li><code>git init</code><br />
    <ul>
        <li>create local repo</li>
        <li>use git add/commit/branch to work locally</li>
    </ul>
    </li>
</ul>
<p><strong>Remote data</strong></p>
<ul>
    <li><code>git remote add name path-to-repo</code>
    <ul>
        <li>track a remote repo (usually &#8220;origin&#8221;) from an existing repo</li>
        <li>remote branches are &#8220;origin/master&#8221;, &#8220;origin/dev&#8221; etc.<strong><br />
        </strong></li>
    </ul>
    </li>
    <li><code>git branch -a</code>
    <ul>
        <li>list all branches (remote and local)</li>
    </ul>
    </li>
</ul>
<ul>
    <li><code>git clone path-to-repo</code>
    <ul>
        <li>create a new local git repo copied from a remote one</li>
        <li>local master tracks remote master</li>
    </ul>
    </li>
    <li><code>git pull </code>
    <ul>
        <li>merge changes from tracked remote branch (if in dev, pull from origin/dev)</li>
    </ul>
    </li>
    <li><code>git push</code>
    <ul>
        <li>send changes to tracked remote branch (if in dev, push to origin/dev)</li>
    </ul>
    </li>
</ul>
<p><strong>Why local and remote?</strong> Subversion has central checkins, so you avoid committing unfinished work. With git, local commits are frequent and you only push when ready.</p>
<h2><span class="caps">GUID</span>s are <span class="caps">GOOD</span></h2>
<p>Git addresses information by a hash (<a title="http://betterexplained.com/articles/the-quick-guide-to-guids/" class=" external" href="http://betterexplained.com/articles/the-quick-guide-to-guids/"><span class="caps">GUID</span></a>) of its contents. If two branches are the same, they have the same <span class="caps">GUID </span>(and vice versa).</p>
<p>Why&#8217;s this cool? We can create branches independently, merge them, and have a common <span class="caps">GUID.</span> No central numbering needed. Usually, we just compare the first few digits: &#8220;Are you on a93?&#8221;.</p>
<h2>Tips &amp; Tricks</h2>
<p>For your .gitconfig:</p>


<pre>
<code>
[alias]
        ci = commit
        st = status
        co = checkout
        oneline = log --pretty=oneline
        br = branch
        la = log --pretty=\"format:%ad %h (%an): %s\" --date=short
</code>
</pre>


<p>There are some <span class="caps">GUI </span>tools for git, but I prefer to learn via the command line.&nbsp;Git is opinionated software (which I like), and analogies help me understand its world view.</p><img src="http://feeds.feedburner.com/~r/Betterexplained/~4/u8znQZCnfJg" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://betterexplained.com/articles/aha-moments-when-learning-git/feed/</wfw:commentRss>
		<slash:comments>19</slash:comments>
		<feedburner:origLink>http://betterexplained.com/articles/aha-moments-when-learning-git/</feedburner:origLink></item>
		<item>
		<title>Fun With Modular Arithmetic</title>
		<link>http://feedproxy.google.com/~r/Betterexplained/~3/tDbaUxh50T0/</link>
		<comments>http://betterexplained.com/articles/fun-with-modular-arithmetic/#comments</comments>
		<pubDate>Mon, 01 Feb 2010 19:55:39 +0000</pubDate>
		<dc:creator>Kalid</dc:creator>
				<category><![CDATA[Math]]></category>
		<category><![CDATA[arithmetic]]></category>
		<category><![CDATA[modulo]]></category>
		<category><![CDATA[numbers]]></category>

		<guid isPermaLink="false">http://betterexplained.com/?p=579</guid>
		<description><![CDATA[A reader recently suggested I write about modular arithmetic (aka &#8220;taking the remainder&#8221;). I hadn&#8217;t given it much thought, but realized the modulo is extremely powerful: it should be in our mental toolbox next to addition and multiplication.
Instead of hitting you in the face with formulas, let&#8217;s explore an idea we&#8217;ve been subtly exposed to [...]]]></description>
			<content:encoded><![CDATA[<p>A reader recently suggested I write about modular arithmetic (aka &#8220;taking the remainder&#8221;). I hadn&#8217;t given it much thought, but realized the modulo is extremely powerful: it should be in our mental toolbox next to addition and multiplication.</p>
<p>Instead of hitting you in the face with formulas, let&#8217;s explore an idea we&#8217;ve been subtly exposed to for years. There&#8217;s a <a class=" external" title="http://www.math.rutgers.edu/~erowland/modulararithmetic.html" href="http://www.math.rutgers.edu/~erowland/modulararithmetic.html">nice article on modular arithmetic</a> that inspired this post.</p>
<h3>Odd, Even and Threeven</h3>
<p>Shortly after discovering whole numbers (1, 2, 3, 4, 5&#8230;) we realized they fall into two groups:</p>
<ul>
    <li>Even: divisible by 2 (0, 2, 4, 6..)</li>
    <li>Odd: not divisible by 2 (1, 3, 5, 7&#8230;)</li>
</ul>
<p>Why&#8217;s this distinction important? It&#8217;s the beginning of abstraction &#8212; we&#8217;re noticing the <em>properties</em> of a number (like being even or odd) and not just the number itself (&#8221;37&#8243;).</p>
<p>This is huge &#8212; it lets us explore math at a deeper level and find relationships between <em>types</em> of numbers, not specific ones. For example, we can make rules like this:</p>
<ul>
    <li>Even x Even = Even</li>
    <li>Odd x Odd = Odd</li>
    <li>Even x Odd = Even</li>
</ul>
<p>These rules are general &#8212; they work at the property level. (Intuitively, I have a <a class=" external" title="http://betterexplained.com/articles/another-look-at-prime-numbers/" href="http://betterexplained.com/articles/another-look-at-prime-numbers/">chemical analogy</a> that &#8220;evenness&#8221; is a molecule some numbers have, and cannot be removed by multiplication.)</p>
<p>But even/odd is a very specific property: division by 2. What about the number 3? How about this:</p>
<ul>
    <li>&#8220;Threeven&#8221; means a number is divisbile by 3 (0, 3, 6, 9&#8230;)</li>
    <li>&#8220;Throdd&#8221; means you are <em>not</em>&nbsp;divisible by 3 (1, 2, 4, 5, 7, 8&#8230;)</li>
</ul>
<p>Weird, but workable. You&#8217;ll notice a few things: there&#8217;s two types of throdd. A number like &#8220;4&#8243; is 1 away from being threeven (remainder 1), while the number 5 is two away (remainder 2).</p>
<p>Being &#8220;threeven&#8221; is just another property of a number. Perhaps not as immediately useful as even/odd, but it&#8217;s there: we can make rules like &#8220;threeven x threeven = threeven&#8221; and so on.</p>
<p>But it&#8217;s getting crazy. We can&#8217;t make new words all the time.</p>
<h3>Enter the Modulo</h3>
<p>The modulo operation (abbreviated &#8220;mod&#8221;, or &#8220;%&#8221; in many programming languages) is the remainder when dividing. For example,&nbsp;&#8221;5 mod 3 = 2&#8243; which means 2 is the remainder when you divide 5 by 3.</p>
<p>Converting everyday terms to math, an &#8220;even number&#8221; is one where it&#8217;s &#8220;0 mod 2&#8243; &#8212; that is, it has a remainder of 0 when divided by 2. An odd number is &#8220;1 mod 2&#8243; (has remainder 1).</p>
<p>Why&#8217;s this cool? Well, our &#8220;odd/even&#8221; rules become this:</p>
<ul>
    <li style="list-style-type: disc; ">Even x Even = 0 x 0 = 0 [even]</li>
    <li style="list-style-type: disc; ">Odd x Odd = 1 x 1 = 1 [odd]</li>
    <li style="list-style-type: disc; ">Even x Odd = 0 x 1 = 0 [even]</li>
</ul>
<p>Cool, huh? Pretty easy to work out &#8212; we converted &#8220;properties&#8221; into actual equations and found some new facts.</p>
<p>What&#8217;s even x even x odd x odd? Well, it&#8217;s 0 x 0 x 1 x 1 = 0. In fact, you can see if there&#8217;s an even being multiplied <em>anywhere </em>the entire result is going to be zero&#8230; I mean even <img src='http://betterexplained.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> .</p>
<h3>Clock Math</h3>
<p>The sneaky thing about modular math is we&#8217;ve already been using it for keeping time &#8212; sometimes called &#8220;clock arithmetic&#8221;.</p>
<p>For example: it&#8217;s 7:00 (am/pm doesn&#8217;t matter). Where will the hour hand be in 7 hours?</p>
<p>Hrm. 7 + 7 = 14, but we can&#8217;t show &#8220;14:00&#8243; on a clock. So it must be 2. We do this reasoning intuitively, and in math terms:</p>
<ul>
    <li>(7 + 7) mod 12 = (14) mod 12 = 2 mod 12 [2 is the remainder when 14 is divided by 12]</li>
</ul>
<p>The equation &#8220;14 mod 12 = 2 mod 12&#8243; means, &#8220;14 o&#8217;clock&#8221; and &#8220;2 o&#8217;clock&#8221; look the same on a 12-hour clock. They are <strong>congruent</strong>, indicated by a triple-equals sign: 14 ≡ 2 mod 12.</p>
<p>Another example: it&#8217;s 8:00. Where will the big hand be in 25 hours?</p>
<p>Instead of adding 25 to 8, you might realize that 25 hours is just &#8220;1 day + 1 hour&#8221;. So, the clock will end up 1 hour ahead, at 9:00.</p>
<ul>
    <li>(8 + 25) mod 12 ≡ (8) mod 12 + (25) mod 12 ≡ (8) mod 12 + (1) mod 12 ≡ 9 mod 12</li>
</ul>
<p>You intuitively converted 25 to 1, and added that to 8.</p>
<h3>Fun Property: Math just works</h3>
<p>Using clocks as an analogy, we can figure out whether the rules of modular arithmetic &#8220;just work&#8221; (they do).</p>
<p><strong>Addition/Subtraction</strong></p>
<p>Let&#8217;s say two times look the same on our clock (&#8221;2:00&#8243;&nbsp;and &#8220;14:00&#8243;).&nbsp;If we add the same &#8220;x&#8221; hours to both, what happens?</p>
<p>Well, they change to the same amount on the clock! 2:00 +&nbsp;5 hours ≡ 14:00 +&nbsp;5 hours &#8212; both will show 7:00.</p>
<p>Why? Well, we never cared about the excess &#8220;12:00&#8243; that the 14 was carrying around. We can just add 5 to the 2 remainder that both have, and they advance the same. For all congruent numbers (2 and 14), adding and subtracting has the same result.</p>
<p><strong>Multiplication</strong></p>
<p>It&#8217;s harder to see whether multiplication stays the same. If 14 ≡ 2 (mod 12), can we multiply both sides and get the same result?</p>
<p>Let&#8217;s see &#8212; what happens when we multiply by 3?</p>
<p>Well, 2:00 * 3 ≡ 6:00. But what&#8217;s &#8220;14:00&#8243; * 3?</p>
<p>Remember, 14 = 12 +&nbsp;2. So, we can say</p>
<ul>
    <li>14 *&nbsp;3 = (12 + 2) *&nbsp;3 = (12 * 3) +&nbsp;(2 *&nbsp;3) mod 12</li>
</ul>
<p>The first part (12 *&nbsp;3)&nbsp;can be ignored! The &#8220;12 hour overflow&#8221; that 14 is carrying around just gets repeated a few times. But who cares? We ignore the overflow anyway.</p>
<p>When multiplying, it&#8217;s only the remainder that matters, which is the same 2 hours for 14:00 and 2:00. Intuitively, this is how I see that multiplication doesn&#8217;t change relationships with modular math (you can multiply both sides of a modular relationship and get the same result). See the <a class=" external" title="http://www.math.rutgers.edu/~erowland/modulararithmetic.html" href="http://www.math.rutgers.edu/~erowland/modulararithmetic.html">above link</a> for more rigorous proofs &#8212; these are my <a class=" external" title="http://betterexplained.com/articles/learning-to-learn-pencil-then-ink/" href="http://betterexplained.com/articles/learning-to-learn-pencil-then-ink/">intuitive pencil lines</a>.</p>
<h3>Uses Of Modular Arithmetic</h3>
<p>Now the fun part &#8212; why is modular arithmetic useful?</p>
<p><strong>Simple time calculations</strong></p>
<p>We do this intuitively, but it&#8217;s nice to give it a name. You have a flight arriving at 3pm. It&#8217;s getting delayed 14 hours. What time will it land?</p>
<p>Well, 14 ≡ 2 mod 12. So I&nbsp;think of it as &#8220;2 hours and an am/pm switch&#8221;, so I know it will be &#8220;3 +&nbsp;2 = 5am&#8221;.</p>
<p>This is a bit more involved than a plain modulo operator, but the principle is the same.</p>
<p><strong>Putting Items In Random Groups </strong></p>
<p>Suppose you have people who bought movie tickets, with a confirmation number. You want to divide them into 2 groups.</p>
<p>What do you do? &#8220;Odds over here, evens over there&#8221;. You don&#8217;t need to know how many tickets were issued (first half, second half), everyone can figure out their group instantly (without contacting a central authority), and the scheme works as more people buy tickets.</p>
<p>Need 3 groups? Divide by 3 and take the remainder (aka mod 3). You&#8217;ll have groups &#8220;0&#8243;, &#8220;1&#8243; and &#8220;2&#8243;.</p>
<p>In programming, taking the modulo is how you can fit items into a hash table: if your table has N entries, convert the item key to a number, do mod N, and put the item in that bucket (perhaps keeping a linked list there). As your hash table grows in size, you can recompute the modulo for the keys.</p>
<p><strong>Picking A Random Item</strong></p>
<p>I use the modulo in real life. Really. We have 4 people playing a game and need to pick someone to go first. <em>Play the mod N mini-game!</em> Give people numbers 0, 1, 2, and 3.</p>
<p>Now everyone goes &#8220;one, two, three, shoot!&#8221; and puts out a random number of fingers. Add them up and divide by 4 &#8212; whoever gets the remainder exactly goes first. (For example: if the sum of fingers is 11, whoever had &#8220;3&#8243; gets to go first, since 11 mod 4 = 3).</p>
<p>It&#8217;s fast and it works.</p>
<p><strong>Running Tasks On A Cycle</strong></p>
<p>Suppose tasks need to happen on a certain schedule:</p>
<ul>
    <li>Task A runs 3x/hour</li>
    <li>Task B runs 6x/hour</li>
    <li>Task C runs 1x/hour</li>
</ul>
<p>How do you store this information and make a schedule? One way:</p>
<ul>
    <li>Have a timer running every minute (keep track of the minute as &#8220;n&#8221;)</li>
    <li>3x / hour means once every 60/3 = 20 minutes. So task A runs whenever &#8220;n % 20 == 0&#8243;</li>
    <li>Task B runs whenever &#8220;n % 10 == 0&#8243;</li>
    <li>Task C runs whenever &#8220;n % 60 == 0&#8243;</li>
</ul>
<p>Oh, you need task C1 which runs 1x per hour, but not the same time as task C?&nbsp;Sure, have it run when &#8220;n mod 60 == 1&#8243; (still once per hour, but not the same as C1).</p>
<p>Mentally I see a cycle I want to &#8220;hit&#8221;&nbsp;at various intervals, so I insert a mod. The neat thing is that the hits can overlap independently. It&#8217;s a bit like XOR in that regard (each XOR can be layered &#8212; but that&#8217;s another article!).</p>
<p>Similarly, when programming you can print every 100th log item by doing:&nbsp;if (n % 100 == 0){ print&#8230; }.</p>
<p>It&#8217;s a very flexible, simple way to have items run on a schedule. In fact, it&#8217;s the way to answer the <a class=" external" title="http://www.codinghorror.com/blog/archives/000781.html" href="http://www.codinghorror.com/blog/archives/000781.html">FizzBuzz sanity check</a>. If you don&#8217;t have the modulo operation in your batbelt the question becomes much more tricky.</p>
<p><strong>Finding Properties Of Numbers</strong></p>
<p>Suppose I told you this:</p>
<ul>
    <li>a = (47 *&nbsp;2 *&nbsp;3)</li>
</ul>
<p>What can you deduce quickly? Well, &#8220;a&#8221; must be even, since it&#8217;s equal to something which involves multiplication by 2.</p>
<p>If I also told you:</p>
<ul>
    <li>a = (39 *&nbsp;7)</li>
</ul>
<p>You&#8217;d balk. Not because you &#8220;know&#8221; the two products are different, but because one is clearly even, and the other is odd. There&#8217;s a problem: a can&#8217;t be the same number in both since the <em>properties don&#8217;t match up</em>.</p>
<p>Things like &#8220;even&#8221;, &#8220;threeven&#8221; and &#8220;mod n&#8221;&nbsp;are properties that are more general than individual numbers, and which we can check for consistency. So we can use modulo to figure out whether numbers are consistent, without knowing what they are!</p>
<p>If I tell you this:</p>
<ul>
    <li>3a + 5b = 8</li>
    <li>3a + b = 2</li>
</ul>
<p>Can these equations be solved with the integers? Let&#8217;s see:</p>
<ul>
    <li>3a + 5b = 8&#8230; let&#8217;s &#8220;mod 3 it&#8221;: 0 + 2b ≡ 2 mod 3, or b ≡ 1 mod 3</li>
    <li>3a + b = 2&#8230; let&#8217;s &#8220;mod 3 it&#8221;: 0 + b ≡ 2 mod 3), or b ≡ 2 mod 3</li>
</ul>
<p>A contradication, good fellows! B can&#8217;t be both &#8220;1 mod 3&#8243; and &#8220;2 mod 3&#8243; &#8212; it&#8217;s as absurd as being even and odd at the same time!</p>
<p>But there&#8217;s one gotcha: numbers like &#8220;1.5&#8243; are neither even nor odd &#8212; they aren&#8217;t integers! The modular properties apply to integers, so what we can say is that b cannot be an <em>integer</em>.</p>
<p>Because, in fact, we can solve that equation:</p>
<ul>
    <li>(3a + 5b) &#8211; (3a +b) = 8 &#8211; 2</li>
    <li>4b = 6</li>
    <li>b = 1.5</li>
    <li>3a + 1.5 = 2, so 3a = 0.5, and a = 1/6</li>
</ul>
<p>Don&#8217;t get seduced by the power of modulo! Know its limits: it applies to integers.</p>
<p><strong>Cryptography</strong></p>
<p>Playing with numbers has very important uses in cryptography. It&#8217;s too much to cover here, but modulo is used in <a href="http://en.wikipedia.org/wiki/Diffie%E2%80%93Hellman_key_exchange" title="http://en.wikipedia.org/wiki/Diffie–Hellman_key_exchange" class=" external">Diffie-Hellman Key Exchange</a> &#8212; used in setting up SSL connections to encrypt web traffic.</p>
<p><strong>Plain English</strong></p>
<p>Geeks love to use technical words in regular contexts. You might hear &#8220;X is the same as Y modulo Z&#8221;&nbsp;which means roughly &#8220;Ignoring Z, X and Y are the same.&#8221;</p>
<p>For example:</p>
<ul>
    <li>b and B are identical, modulo capitalization</li>
    <li>The iTouch and iPad are identical, modulo size <img src='http://betterexplained.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </li>
</ul>
<h3>Onward and Upward</h3>
<p>It&#8217;s strange thinking about the &#8220;utility&#8221;&nbsp;of the modulo operator &#8212; it&#8217;s like someone asking why exponents are useful. In everyday life, not very, but it&#8217;s a tool to understand patterns in the world, and create your own.</p>
<p>In general, I see a few general use cases:</p>
<ul>
    <li>Range reducer:&nbsp;take an input, mod N, and you have a number from 0 to N-1.</li>
    <li>Group assigner:&nbsp;take an input, mod&nbsp;N, and you have it tagged as a group from 0 to N-1. This group can be agreed upon by any number of parties &#8212; for example, different servers that know N = 20 can agree what group ID=57 belongs to.</li>
    <li>Property deducer: treat numbers according to properties (even, threeven, and so on) and work out principles derived at the property level</li>
</ul>
<p>I&#8217;m sure there&#8217;s dozens more uses I&#8217;ve missed &#8212; feel free to comment below. Happy math!</p><img src="http://feeds.feedburner.com/~r/Betterexplained/~4/tDbaUxh50T0" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://betterexplained.com/articles/fun-with-modular-arithmetic/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		<feedburner:origLink>http://betterexplained.com/articles/fun-with-modular-arithmetic/</feedburner:origLink></item>
		<item>
		<title>Learning To Learn: Pencil, Then Ink</title>
		<link>http://feedproxy.google.com/~r/Betterexplained/~3/CpBXxeMDoYY/</link>
		<comments>http://betterexplained.com/articles/learning-to-learn-pencil-then-ink/#comments</comments>
		<pubDate>Thu, 14 Jan 2010 13:14:09 +0000</pubDate>
		<dc:creator>Kalid</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[drawing]]></category>
		<category><![CDATA[learning]]></category>

		<guid isPermaLink="false">http://betterexplained.com/?p=537</guid>
		<description><![CDATA[I loved drawing as a kid. A recent "aha!" was realizing how similar the process of good drawing is to good learning --  they depend on recognizing and mastering underlying structures. My philosophy in 3 words:

Pencil, then ink.

It's simple, perhaps cliched, but powerful. Lee Ames had a great series of books on drawing (Draw [...]]]></description>
			<content:encoded><![CDATA[<p>I loved drawing as a kid. A recent "aha!" was realizing how similar the process of good drawing is to good learning --  they depend on recognizing and mastering underlying structures. My philosophy in 3 words:</p>

<p><strong>Pencil, then ink.</strong></p>

<p>It's simple, perhaps cliched, but powerful. Lee Ames had a great series of books on drawing (<a href="http://www.draw50.com/animals.htm">Draw 50 animals</a>):</p>

<table>
<tr valign="top">
<td valign="top"><img src="http://betterexplained.com/wp-content/uploads/drawing/draw50_400.png" width="250" />(<a href="http://www.amazon.com/Draw-50-Animals-Lee-Ames/dp/0385195192">amazon</a>)</td>
<td valign="top"><img src="http://betterexplained.com/wp-content/uploads/drawing/draw50_sample.png" /> (<a href="http://www.draw50.com/animals.htm">source</a>)</td>
</tr>
</table>

<p>The cover reveals it all. How do you draw an elephant?</p>


<ul>
<li><em>Pencil the structure</em> using ovals, rectangles, and so on</li>
<li><em>Ink the final result</em>, taking the lines you want</li>
<li><em>Erase</em> the underlying pencil structure, revealing the elephant</li>
</ul>



<p>Why's this special? <strong>The key to learning is understanding the pencil structure</strong> -- the scaffolding that's not always present in that final, finished elephant. Let's see how this analogy relates to learning.</p>

<h2>Tracing Math</h2>

<p>Is tracing different from drawing? You bet. Tracing is mimicry -- we don't know <em>why</em> a line is there. We just start in one corner and work our way around. Sure, we might make a pretty elephant -- but can we draw one with a different trunk? Standing on two hind legs? Probably not.</p>

<p>Math is similar: we "teach" by tracing a student through the steps of a proof. But there's an <em>underlying pencil structure</em> that was in the mind's eye of the proof's author that we're not seeing. We're walking the student along the drawing ("Here is the head, here is the trunk, here is the leg") without show the <em>mindset</em> that created the proof ("The head is an oval, connected to a larger oval for the body; the legs are cylinders, which we smooth out.").</p>

<p>If we're lucky, the student generalizes the steps and creates their own pencil structure.</p>

<p><strong>But why?</strong> Why do we leave the most interesting part of understanding to private contemplation? I love discovering these "aha" moments that put the result into place -- what's the mental map that made the facts snap together?</p>

<p>When we share insights we can stop "tracing math" and begin drawing on our own. It's way more satisfying.</p>

<h2>Creation Vs. Understanding</h2>

<p>What's the point of education: the results or understanding?</p>

<p>If the goal are results, then art class should be about using stencils and tracing to make perfect representations. If the goal is understanding, then we should take out the pencils, make our lumpy apples and lopsided bananas and try our hand at still lifes.</p>

<p>It's seldom either-or: we want results <strong>and</strong> understanding. Unfortunately, we focus on results because they're easier to test (Can you plug X into these formulas and get the right answer?). I'm here to remind us that we need to understand what's happening too.</p>

<h2>Rigor and Intuition</h2>

<p>I've struggled how to reconcile rigor and intuition -- both have their role, but how do they fit? The drawing analogy captures my feelings:</p>


<ul>
<li>Rigor (permanent inked lines) helps cement ideas <strong>after</strong> the intuitive pencil structure has been put into place</li>
</ul>



<p>Focusing on rigor prematurely creates fear and trepidation -- "What if I'm wrong?", and encourages people to trace the inked results instead of learning how to experiment on their own. It makes you think math (or any subject) is something you get right or not at all. Which isn't the case -- many (most?) results have been developed intuitively and cemented later.</p>

<p>Rigor/ink is emphasized because it's the only thing visible; I want to champion the (now-invisible) pencil lines which laid the original groundwork.</p>

<h2>The Myth Of The Perfect Formula</h2>

<p>Before seeing the Ames book, I thought you drew by starting in one corner and filling in the figure. Some experts may do that (more later), but the "normal way" to draw is by starting with a penciled foundation.</p>

<p>We know writers need drafts. But do we allow drafts in learning? Are we so concerned with reproducing inked results that we discourage or ignore the pencil?</p>

<p>Math developed through wayward paths and missed connections, not always by the smooth progression we see in our classroom syllabus. Showing only the final results makes it appear like it's <em>supposed</em> to progress linearly and unwaveringly. Maybe discussing how zero, negatives, and <a href="http://betterexplained.com/articles/a-visual-intuitive-guide-to-imaginary-numbers/">imaginary numbers</a> were initially distrusted (and embraced) would help us empathize with students embracing the idea. </p>

<p>I chuckle that we "matter of factly" introduce imaginary numbers when the experts of the time had objections. They're difficult, non-intuitive concepts (at first) -- it's ok to admit that we had some rough drafts crumpled in the corner.</p>

<p>Wayward paths can help us better understand the correct ones.</p>

<h2>Learning: Seeing the Structure</h2>

<p>After my frustrations with learning new concepts, I've taken the philosophy that <em>some</em> structure must exist. When I see a new concept (an inked bird, for example) I really think there <em>must</em> be some collection of shapes that make it make sense. </p>

<p>If I'm having trouble, I blame my approach -- I'm just not seeing the idea in the same way as its inventor. Maybe someone else has written about it, or there's an analogy to another idea.</p>

<p>But what if there's no underlying structure, just a perfect, inked elephant without eraser marks? It's possible.</p>

<p>After you internalize an idea, you start thinking directly in ink. We don't "draw" the letter A in pencil -- we just write A because we're so familiar with it. Practice moves ideas into the "ink-only" stage, which let us work on bigger ideas. For example, you need need to commit arithmetic to "muscle memory" before you can understand algebra. If you can write arithmetic, you can learn to draw algebra. Once you can write algebra, you can draw calculus. And so on -- if you don't get arithmetic, Calculus (with its pencil-lines in algebra) will still look like a jumble.</p>

<h2>Revealing Structures</h2>

<p>We often look back and add the original pencil lines to finished works:</p>


<ul>
<li><a href="http://en.wikipedia.org/wiki/Design_pattern_(computer_science)">Design Patterns</a> in programming -- abstract ways to find similarities between programming solutions</li>
<li><a href="http://en.wikipedia.org/wiki/Grid_(page_layout)">Grid Layout</a> in graphic design: A general structure to organize content</li>
<li><a href="http://en.wikipedia.org/wiki/Monomyth">Monomyth</a> in storytelling -- a common pattern popular stories take</li>
</ul>



<p>Sometimes we create "nice-looking elephants" through trial and error. Later on, we realize there's a common structure that can simplify future efforts. True learning is about discovering and exploring these structures, not simply generating the pretty elephants.</p>

<h2>Do Experts Teach Best?</h2>

<p>Who should teach? The person who just "sees" the elephant from day 1, or the one who learned to break it down and construct it? Imagine taking an art class from <a href="http://en.wikipedia.org/wiki/Stephen_Wiltshire">Stephen Wiltshire</a> (this <a href="http://www.stephenwiltshire.co.uk/Rome_Panorama_by_Stephen_Wiltshire.aspx">panorama of Rome</a> was drawn from memory):</p>

<p><object width="425" height="350"><param name="movie" value="http://www.youtube.com/v/ckqDX2XpdyY"></param><param name="wmode" value="transparent"></param><embed src="http://www.youtube.com/v/ckqDX2XpdyY" type="application/x-shockwave-flash" wmode="transparent" width="425" height="350"></embed></object> </p>

<p>Drawing a city after a helicopter ride is an amazing gift -- but I doubt it's transferrable. He goes far beyond the underlying pencil structure that "regular" artists would need.</p>

<p>Beginners need the pencil marks -- experts who've internalized them sometimes forget that. True learning happens when people can recreate that structure in their minds. When the experts can remember what it was like to not "see it all at once", then real learning can happen.</p>

<p>I want to share the pencil sketches that evolved into the elephant, instead of erasing them and pretending that I, too, can just draw from memory.</p>

<h2>Final Thoughts</h2>

<p>I'm sure there's more analogies hidden in there somewhere. The process of drawing -- pencil structure, inked result -- captures thoughts about learning that have been rattling in my head.</p>

<p>Don't learn by tracing: find (or invent!) those pencil structures. Seeing the pencil lines makes the idea become your own: you can modify it, combine it with others, or just appreciate it at a deeper level. And that's the joy of learning.</p><img src="http://feeds.feedburner.com/~r/Betterexplained/~4/CpBXxeMDoYY" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://betterexplained.com/articles/learning-to-learn-pencil-then-ink/feed/</wfw:commentRss>
		<slash:comments>18</slash:comments>
		<feedburner:origLink>http://betterexplained.com/articles/learning-to-learn-pencil-then-ink/</feedburner:origLink></item>
	</channel>
</rss><!-- Dynamic page generated in 0.813 seconds. --><!-- Cached page generated by WP-Super-Cache on 2010-03-11 14:31:26 -->
