<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	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/"
	>

<channel>
	<title>Dreamshire &#187; Binomials</title>
	<atom:link href="http://blog.dreamshire.com/tag/binomials/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.dreamshire.com</link>
	<description>Showing what's behind the curtain</description>
	<lastBuildDate>Wed, 01 Sep 2010 02:23:55 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Project Euler Problem 203 Solution</title>
		<link>http://blog.dreamshire.com/2009/04/20/project-euler-problem-204-solution/</link>
		<comments>http://blog.dreamshire.com/2009/04/20/project-euler-problem-204-solution/#comments</comments>
		<pubDate>Tue, 21 Apr 2009 04:25:13 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[Project Euler Solutions]]></category>
		<category><![CDATA[Binomials]]></category>
		<category><![CDATA[Perl]]></category>
		<category><![CDATA[Project Euler]]></category>
		<category><![CDATA[solution]]></category>

		<guid isPermaLink="false">http://blog.dreamshire.com/?p=822</guid>
		<description><![CDATA[Squarefree Binomial Coefficients]]></description>
			<content:encoded><![CDATA[<h4><u>Problem Description</u></h4>
<p>The binomial coefficients <img src="" style="display:none;" alt="^(" /><sup>n</sup>C<sub>k</sub> can be arranged in triangular form, Pascal&#8217;s triangle, like this:</p>
<div style="text-align:center;">
<table align="center">
<tr>
<td colspan="7"></td>
<td>1</td>
<td colspan="7"></td>
</tr>
<tr>
<td colspan="6"></td>
<td>1</td>
<td></td>
<td>1</td>
<td colspan="6"></td>
</tr>
<tr>
<td colspan="5"></td>
<td>1</td>
<td></td>
<td>2</td>
<td></td>
<td>1</td>
<td colspan="5"></td>
</tr>
<tr>
<td colspan="4"></td>
<td>1</td>
<td></td>
<td>3</td>
<td></td>
<td>3</td>
<td></td>
<td>1</td>
<td colspan="4"></td>
</tr>
<tr>
<td colspan="3"></td>
<td>1</td>
<td></td>
<td>4</td>
<td></td>
<td>6</td>
<td></td>
<td>4</td>
<td></td>
<td>1</td>
<td colspan="3"></td>
</tr>
<tr>
<td colspan="2"></td>
<td>1</td>
<td></td>
<td>5</td>
<td></td>
<td>10</td>
<td></td>
<td>10</td>
<td></td>
<td>5</td>
<td></td>
<td>1</td>
<td colspan="2"></td>
</tr>
<tr>
<td colspan="1"></td>
<td>1</td>
<td></td>
<td>6</td>
<td></td>
<td>15</td>
<td></td>
<td>20</td>
<td></td>
<td>15</td>
<td></td>
<td>6</td>
<td></td>
<td>1</td>
<td colspan="1"></td>
</tr>
<tr>
<td>1</td>
<td></td>
<td>7</td>
<td></td>
<td>21</td>
<td></td>
<td>35</td>
<td></td>
<td>35</td>
<td></td>
<td>21</td>
<td></td>
<td>7</td>
<td></td>
<td>1</td>
</tr>
</table>
</div>
<p>It can be seen that the first eight rows of Pascal&#8217;s triangle contain twelve distinct numbers: 1,&nbsp;2,&nbsp;3,&nbsp;4,&nbsp;5,&nbsp;6,&nbsp;7,&nbsp;10,&nbsp;15,&nbsp;20,&nbsp;21&nbsp;and&nbsp;35.</p>
<p>A positive integer <var>n</var> is called squarefree if no square of a prime divides <var>n</var>.<br />
Of the twelve distinct numbers in the first eight rows of Pascal&#8217;s triangle, all except 4 and 20 are squarefree.<br />
The sum of the distinct squarefree numbers in the first eight rows is 105.</p>
<p>Find the sum of the distinct squarefree numbers in the first 51 rows of Pascal&#8217;s triangle.</p>
<h4><u>Analysis</u></h4>
<p>Only a few primes are required.  For example,  51! is the biggest numerator (<sub>n</sub>C<sub>k</sub> = n! / k! (n-k)! ) and the largest prime factor required is &le; &radic;51 which is 7.  We added a few more to try bigger problems.</p>
<p>So we fill a hash&#8217;s keys with unique numbers from Pascal&#8217;s triangle and iterate through the 589 binomial coefficients and track which ones are squarefree, sum them up and print the results.</p>
<h4><u>Solution</u></h4>
<p>Runs < 1 second in Perl.</p>

<div class="wp_syntax"><div class="code"><pre class="perl" style="font-family:monospace;"><span style="color: #0000ff;">@primes</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">3</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">5</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">7</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">11</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">13</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">17</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">19</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #0000ff;">$rows</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">51</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">#more rows means more primes</span>
<span style="color: #0000ff;">$s</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">#use a hash, b, to select the distinct binomial coefficients from Pascal's triangle</span>
<span style="color: #b1b100;">while</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">@_</span><span style="color: #339933;">=</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span><span style="color: #000066;">map</span><span style="color: #0000ff;">$_</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">$_</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">+</span><span style="color: #0000ff;">$_</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">$_</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>1<span style="color: #339933;">..</span><span style="color: #0000ff;">@_</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">&lt;=</span><span style="color: #0000ff;">$rows</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> <span style="color: #000066;">grep</span> <span style="color: #0000ff;">$b</span><span style="color: #009900;">&#123;</span><span style="color: #0000ff;">$_</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">++,</span> <span style="color: #0000ff;">@_</span><span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #b1b100;">for</span> <span style="color: #0000ff;">$x</span> <span style="color: #009900;">&#40;</span><span style="color: #000066;">keys</span> <span style="color: #0000ff;">%b</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #0000ff;">$s</span><span style="color: #339933;">+=</span><span style="color: #0000ff;">$x</span> <span style="color: #b1b100;">if</span> sqf<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$x</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000066;">print</span> <span style="color: #ff0000;">&quot;Answer to PE203 = $s&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">sub</span> sqf <span style="color: #009900;">&#123;</span> <span style="color: #000066;">return</span> <span style="color: #339933;">!</span> <span style="color: #000066;">grep</span> <span style="color: #0000ff;">@_</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">%</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$_</span><span style="color: #339933;">*</span><span style="color: #0000ff;">$_</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">==</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">@primes</span> <span style="color: #009900;">&#125;</span></pre></div></div>

<h4><u>Comments</u></h4>
<p>Wanton use of grep and map.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.dreamshire.com/2009/04/20/project-euler-problem-204-solution/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
