<?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; factorials</title>
	<atom:link href="http://blog.dreamshire.com/tag/factorials/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 121 Solution</title>
		<link>http://blog.dreamshire.com/2009/05/25/project-euler-problem-121-solution/</link>
		<comments>http://blog.dreamshire.com/2009/05/25/project-euler-problem-121-solution/#comments</comments>
		<pubDate>Mon, 25 May 2009 07:18:19 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[Project Euler Solutions]]></category>
		<category><![CDATA[Solutions 100-149]]></category>
		<category><![CDATA[factorials]]></category>
		<category><![CDATA[Generating Functions]]></category>
		<category><![CDATA[Large Integers]]></category>
		<category><![CDATA[Project Euler]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[solution]]></category>

		<guid isPermaLink="false">http://blog.dreamshire.com/?p=1379</guid>
		<description><![CDATA[Investigate the game of chance involving coloured discs.]]></description>
			<content:encoded><![CDATA[<h4><u>Problem Description</u></h4>
<p>A bag contains one red disc and one blue disc. In a game of chance a player takes a disc at random and its colour is noted. After each turn the disc is returned to the bag, an extra red disc is added, and another disc is taken at random.</p>
<p>The player pays &pound;1 to play and wins if they have taken more blue discs than red discs at the end of the game.</p>
<p>If the game is played for four turns, the probability of a player winning is exactly 11/120, and so the maximum prize fund the banker should allocate for winning in this game would be &pound;10 before they would expect to incur a loss. Note that any payout will be a whole number of pounds and also includes the original &pound;1 paid to play the game, so in the example given the player actually wins &pound;9.</p>
<p>Find the maximum prize fund that should be allocated to a single game in which fifteen turns are played.</p>
<h4><u>Analysis</u></h4>
<p>This problem, a series of independent events,  can be solved by a <a href="http://en.wikipedia.org/wiki/Generating_function">ordinary generating function</a> for the numerators of the respective probabilities.  The nth coefficients of (x+1)(x+2)(x+3)&#8230;(x+15) are the sum of the products taken n at a time.  We are only concerned with a winning scenario, which, for this problem, would be selecting 8 or more blue disks.  Therefore, only the top eight coefficients are required.</p>
<p>What&#8217;s left is to take the reciprocal of the sum of the eight coefficients divided by (n+1)!.</p>
<p>Example: for n=7, the equation is (x+1)(x+2)(x+3)(x+4)(x+5)(x+6)(x+7) because we keep adding a red disk after each turn leaving 1 blue disk and 7 red disks for a total of <strong>8 disks</strong> by the end of the game.  This equation expands to:<br />
x<sup>7</sup> + 28x<sup>6</sup> + 322x<sup>5</sup> + 1960x<sup>4</sup> + 6769x<sup>3</sup>  + 13132x<sup>2</sup> + 13068x + 5040.<br />
A winning scenario is selecting the blue disk 4 or more times (7 times out of 7 tries, 6 out of 7, 5 out of 7 or 4 out of 7) with the respective sum of the coefficients 1 + 28 + 322 + 1960 = 2311.  The probability of winning is 2311/8! = 2311/40320.  The prize fund would have to be the reciprocal of the winning probability, 40320/2311 &asymp; &pound;17.</p>
<h4><u>Solution</u></h4>
<p>Runs < 1 second in Python.</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">def</span> fact<span style="color: black;">&#40;</span>n<span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">reduce</span><span style="color: black;">&#40;</span><span style="color: #ff7700;font-weight:bold;">lambda</span> x,y:x<span style="color: #66cc66;">*</span>y,<span style="color: #008000;">range</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">1</span>,n+<span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>,<span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>
&nbsp;
n = <span style="color: #ff4500;">15</span>
r = <span style="color: black;">&#40;</span>n-<span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>/<span style="color: #ff4500;">2</span>
p = <span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span>+<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span><span style="color: #66cc66;">*</span>r
<span style="color: #ff7700;font-weight:bold;">for</span> k <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">range</span><span style="color: black;">&#40;</span>n+<span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>:
  <span style="color: #ff7700;font-weight:bold;">for</span> i <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">range</span><span style="color: black;">&#40;</span>r, <span style="color: #ff4500;">0</span>, -<span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>:
    p<span style="color: black;">&#91;</span>i<span style="color: black;">&#93;</span> += k<span style="color: #66cc66;">*</span>p<span style="color: black;">&#91;</span>i-<span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span>
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Answer to PE121 = &quot;</span>, fact<span style="color: black;">&#40;</span>n+<span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span> / <span style="color: #008000;">sum</span><span style="color: black;">&#40;</span>p<span style="color: black;">&#41;</span></pre></div></div>

<h4><u>Comments</u></h4>
<p>for n=100 the value (34316&#8230;) is calculated less than 1 second.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.dreamshire.com/2009/05/25/project-euler-problem-121-solution/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Project Euler Problem 15 Solution</title>
		<link>http://blog.dreamshire.com/2009/04/19/project-euler-problem-15-solution/</link>
		<comments>http://blog.dreamshire.com/2009/04/19/project-euler-problem-15-solution/#comments</comments>
		<pubDate>Mon, 20 Apr 2009 06:07:44 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[Project Euler Solutions]]></category>
		<category><![CDATA[Solutions 10-19]]></category>
		<category><![CDATA[factorials]]></category>
		<category><![CDATA[Large Integers]]></category>
		<category><![CDATA[Project Euler]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[solution]]></category>

		<guid isPermaLink="false">http://blog.dreamshire.com/?p=770</guid>
		<description><![CDATA[Starting in the top left corner in a 20 by 20 grid, how many routes are there to the bottom right corner?]]></description>
			<content:encoded><![CDATA[<h4><u>Problem Description</u></h4>
<p>Starting in the top left corner of a 2&times;2 grid, there are 6 routes (without backtracking) to the bottom right corner.</p>
<div style="text-align:center;">
<img src="http://www.projecteuler.net/project/images/p_015.gif" alt="" />
</div>
<p>How many routes are there through a 20&times;20 grid?</p>
<h4><u>Analysis</u></h4>
<p>This question has been posed as: &#8220;Starting at the top left corner, how many ways through town are possible using only one-way streets and ending at the bottom right corner of a n x n grid?&#8221;</p>
<p>The solution is the central binomial coefficient or the center number in the 2n+1 row of pascal&#8217;s triangle.  The formula in general for an n x m grid is (n+m)! / (n!m!).  Any combination using nDs and mLs would be a valid move.</p>
<p>In the example provided in the problem description there would be (2+2)! or 24 possible moves, but many would be indistinguishable, and we would have to divide by (2! &middot; 2!) to eliminate them.  So, (2 + 2)! / (2! &middot; 2!) = 24 / 4 = 6.  Namely, LLDD, LDLD, LDDL, DLLD, DLDL and DDLL.  By indistinguishable we mean DDL<sub>1</sub>L<sub>2</sub> is the same move as DDL<sub>2</sub>L<sub>1</sub>.</p>
<h4><u>Solution</u></h4>
<p>Runs < 1 second in Python.</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">f = <span style="color: #ff7700;font-weight:bold;">lambda</span> n:<span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span>,<span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span><span style="color: black;">&#91;</span>n<span style="color: #66cc66;">&gt;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span> <span style="color: #ff7700;font-weight:bold;">or</span> f<span style="color: black;">&#40;</span>n-<span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">*</span>n
n = <span style="color: #ff4500;">20</span>
m = <span style="color: #ff4500;">20</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Answer to PE15 = &quot;</span>, f<span style="color: black;">&#40;</span>n+m<span style="color: black;">&#41;</span> / <span style="color: black;">&#40;</span> f<span style="color: black;">&#40;</span>n<span style="color: black;">&#41;</span><span style="color: #66cc66;">*</span>f<span style="color: black;">&#40;</span>m<span style="color: black;">&#41;</span> <span style="color: black;">&#41;</span></pre></div></div>

<h4><u>Comments</u></h4>
<p>The OEIS for this series is <a href="http://www.research.att.com/~njas/sequences/A000984">A000984</a><br />
If we added the condition &#8220;which do not pass below the diagonal&#8221; to our problem statement, we would use a Catalan number.  It is calculated as 2n! / n! / (n+1)!</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.dreamshire.com/2009/04/19/project-euler-problem-15-solution/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Project Euler Problem 53 Solution</title>
		<link>http://blog.dreamshire.com/2009/04/11/project-euler-problem-53-solution/</link>
		<comments>http://blog.dreamshire.com/2009/04/11/project-euler-problem-53-solution/#comments</comments>
		<pubDate>Sat, 11 Apr 2009 07:30:34 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[Project Euler Solutions]]></category>
		<category><![CDATA[Solutions 50-59]]></category>
		<category><![CDATA[factorials]]></category>
		<category><![CDATA[Perl]]></category>
		<category><![CDATA[Project Euler]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[solution]]></category>

		<guid isPermaLink="false">http://blog.dreamshire.com/?p=592</guid>
		<description><![CDATA[How many values of C(n,r), for 1 ≤ n ≤ 100, exceed one-million?]]></description>
			<content:encoded><![CDATA[<h4><u>Problem Description</u></h4>
<p>There are exactly ten ways of selecting three from five, 12345:</p>
<p style='text-align:center;'>123, 124, 125, 134, 135, 145, 234, 235, 245, and 345</p>
<p>In combinatorics, we use the notation, <sup>5</sup>C<sub>3</sub> = 10.<br />
In general, <sup><i>n</i></sup>C<sub><i>r</i></sub> = <i>n</i>! / <i>r</i>!(<i>n&minus;r</i>)!, where <i>r</i> &le; <i>n</i>, <i>n</i>! = <i>n</i>&times;(<i>n</i>&minus;1)&times;&#8230;&times;3&times;2&times;1, and 0! = 1.<br />
It is not until <i>n</i> = 23, that a value exceeds one-million: <sup>23</sup>C<sub>10</sub> = 1144066.<br />
How many, not necessarily distinct, values of &nbsp;<sup><i>n</i></sup>C<sub><i>r</i></sub>, for 1 &le; <i>n</i> &le; 100, are greater than one-million?</p>
<h4><u>Analysis</u></h4>
<p>There is an efficient way to solve this using Pascal&#8217;s triangle.</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>Each row of this triangle is vertically symmetric, so C(n, r) = C(n, n-r) and any C(n, x) for x from r to (n-r) is greater than C(n, r).  </p>
<p>If C(n, r) > 10<sup>6</sup> then C(n, x) for x from r to (n-r) will also > 10<sup>6</sup>, therefore, the number of C(n, r) > 10<sup>6</sup>,  is simply (n-r)-r+1 for that row <em>n</em>.</p>
<h4><u>Solution</u></h4>
<p>Runs < 1 second in Python.</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">from</span> Euler <span style="color: #ff7700;font-weight:bold;">import</span> binomial
limit, maxn, c = 1e6, <span style="color: #ff4500;">100</span>, <span style="color: #ff4500;">0</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">for</span> n <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">range</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">23</span>, maxn+<span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>:
  <span style="color: #ff7700;font-weight:bold;">for</span> r <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">range</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">2</span>, n/<span style="color: #ff4500;">2</span>+<span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">if</span> binomial<span style="color: black;">&#40;</span>n, r<span style="color: black;">&#41;</span> <span style="color: #66cc66;">&gt;</span> limit:
      c += n + <span style="color: #ff4500;">1</span> - <span style="color: #ff4500;">2</span><span style="color: #66cc66;">*</span>r
      <span style="color: #ff7700;font-weight:bold;">break</span>
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Answer to PE53 = &quot;</span>, c</pre></div></div>

<h4><u>Comments</u></h4>
<ul>
<li>More information on the <em>Euler</em> module can be found on the <a href="http://blog.dreamshire.com/2009/03/26/94/">tools page</a>.</li>
<li>How many, not necessarily distinct, values of &nbsp;<sup><i>n</i></sup>C<sub><i>r</i></sub>, for 1 &le; <i>n</i> &le; 1000, are greater than ten-billion? 490,806
</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://blog.dreamshire.com/2009/04/11/project-euler-problem-53-solution/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Project Euler Problem 74 Solution</title>
		<link>http://blog.dreamshire.com/2009/04/08/project-euler-problem-74-solution/</link>
		<comments>http://blog.dreamshire.com/2009/04/08/project-euler-problem-74-solution/#comments</comments>
		<pubDate>Wed, 08 Apr 2009 21:12:14 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[Project Euler Solutions]]></category>
		<category><![CDATA[Solutions 70-79]]></category>
		<category><![CDATA[factorials]]></category>
		<category><![CDATA[Perl]]></category>
		<category><![CDATA[Project Euler]]></category>
		<category><![CDATA[solution]]></category>

		<guid isPermaLink="false">http://blog.dreamshire.com/?p=444</guid>
		<description><![CDATA[Determine the number of factorial chains that contain exactly sixty non-repeating terms.]]></description>
			<content:encoded><![CDATA[<h4><u>Problem Description</u></h4>
<p>The number 145 is well known for the property that the sum of the factorial of its digits is equal to 145:</p>
<p>1! + 4! + 5! = 1 + 24 + 120 = 145</p>
<p>Perhaps less well known is 169, in that it produces the longest chain of numbers that link back to 169; it turns out that there are only three such loops that exist:</p>
<p>169 &rarr; 363601 &rarr; 1454 &rarr; 169<br />
871 &rarr; 45361 &rarr; 871<br />
872 &rarr; 45362 &rarr; 872</p>
<p>It is not difficult to prove that EVERY starting number will eventually get stuck in a loop. For example,</p>
<p>69 &rarr; 363600 &rarr; 1454 &rarr; 169 &rarr; 363601 (&rarr; 1454)<br />
78 &rarr; 45360 &rarr; 871 &rarr; 45361 (&rarr; 871)<br />
540 &rarr; 145 (&rarr; 145)</p>
<p>Starting with 69 produces a chain of five non-repeating terms, but the longest non-repeating chain with a starting number below one million is sixty terms.</p>
<p>How many chains, with a starting number below one million, contain exactly sixty non-repeating terms?</p>
<h4><u>Analysis</u></h4>
<p>Knowing of a clever solution and debating the use of making one weighed in favor of utilizing some of the code from problem 34.  We wrote a recursive solution that was slow but intuitive (As the the saying goes, &#8216;In order to understand recursion, one must first understand recursion&#8217;).</p>
<p>By placing the factorials in an array saved having to recalculate them as we only needed the first 10.  Also, starting from 70 was a parameter inferred from the problem description. </p>
<p>We did want a solution that was somewhat extensible and able to answer other questions if the need ever arose.  Making changes to bounds or to the size of the series would be easy to change.</p>
<h4><u>Solution</u></h4>
<p>Runs < 55 seconds in Perl.</p>

<div class="wp_syntax"><div class="code"><pre class="perl" style="font-family:monospace;"><span style="color: #b1b100;">my</span> <span style="color: #0000ff;">%sum</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">@fact</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: #cc66cc;">1</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">6</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">24</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">120</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">720</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">5</span>_040<span style="color: #339933;">,</span> <span style="color: #cc66cc;">40</span>_320<span style="color: #339933;">,</span> <span style="color: #cc66cc;">362</span>_880<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$i</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">70</span><span style="color: #339933;">;</span> <span style="color: #0000ff;">$i</span><span style="color: #339933;">&lt;</span><span style="color: #cc66cc;">999999</span><span style="color: #339933;">;</span> <span style="color: #0000ff;">$i</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #0000ff;">%h</span><span style="color: #339933;">=</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #0000ff;">$x60</span><span style="color: #339933;">++</span> <span style="color: #b1b100;">if</span> chains<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$i</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">==</span><span style="color: #cc66cc;">60</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 PE74 = $x60&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">sub</span> chains <span style="color: #009900;">&#123;</span>
  <span style="color: #0000ff;">$n</span> <span style="color: #339933;">=</span> <span style="color: #000066;">shift</span><span style="color: #339933;">;</span>
  <span style="color: #000066;">return</span> <span style="color: #000066;">scalar</span> <span style="color: #000066;">keys</span> <span style="color: #0000ff;">%h</span> <span style="color: #b1b100;">if</span> <span style="color: #0000ff;">$h</span><span style="color: #009900;">&#123;</span><span style="color: #0000ff;">$n</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
  <span style="color: #0000ff;">$h</span><span style="color: #009900;">&#123;</span><span style="color: #0000ff;">$n</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">++;</span>
  <span style="color: #b1b100;">unless</span> <span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">$sum</span><span style="color: #009900;">&#123;</span><span style="color: #0000ff;">$n</span><span style="color: #009900;">&#125;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #0000ff;">$sum</span><span style="color: #009900;">&#123;</span><span style="color: #0000ff;">$n</span><span style="color: #009900;">&#125;</span> <span style="color: #339933;">+=</span> <span style="color: #0000ff;">$fact</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">$_</span><span style="color: #009900;">&#93;</span> <span style="color: #b1b100;">for</span> <span style="color: #000066;">split</span> <span style="color: #339933;">//,</span><span style="color: #0000ff;">$n</span> <span style="color: #009900;">&#125;</span>
<span style="color: #000066;">return</span> chains<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$sum</span><span style="color: #009900;">&#123;</span><span style="color: #0000ff;">$n</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<h4><u>Comments</u></h4>
]]></content:encoded>
			<wfw:commentRss>http://blog.dreamshire.com/2009/04/08/project-euler-problem-74-solution/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Project Euler Problem 34 Solution</title>
		<link>http://blog.dreamshire.com/2009/04/08/project-euler-problem-34-solution/</link>
		<comments>http://blog.dreamshire.com/2009/04/08/project-euler-problem-34-solution/#comments</comments>
		<pubDate>Wed, 08 Apr 2009 07:24:39 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[Project Euler Solutions]]></category>
		<category><![CDATA[Solutions 30-39]]></category>
		<category><![CDATA[factorials]]></category>
		<category><![CDATA[Perl]]></category>
		<category><![CDATA[Project Euler]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[solution]]></category>

		<guid isPermaLink="false">http://blog.dreamshire.com/?p=436</guid>
		<description><![CDATA[Find the sum of all numbers which are equal to the sum of the factorial of their digits.]]></description>
			<content:encoded><![CDATA[<h4><u>Problem Description</u></h4>
<p>145 is a curious number, as 1! + 4! + 5! = 1 + 24 + 120 = 145.</p>
<p>Find the sum of all numbers which are equal to the sum of the factorial of their digits.</p>
<p><small>Note: as 1! = 1 and 2! = 2 are not sums they are not included.</small></p>
<h4><u>Analysis</u></h4>
<p>These type of numbers are referred to as factorions and it&#8217;s easy to learn that only 4 exist: 1, 2, 145 &#038; 40585.  The problem description wants us to ignore 1 &#038; 2 so the answer to this problem should become obvious.</p>
<p>But, let&#8217;s write a program anyway to search for factorions.  As usual, when writing a brute force search algorithm, we must first determine our bounds.  The lower bound is 10 because single digit candidates are to be ignored.  The upper bound we discover as follows (from Wikipedia):</p>
<p style="margin: 30px 30px">If n is a natural number of d digits that is a factorion, then 10<sup>d − 1</sup> ≤ n ≤ 9!d and this fails to hold for d ≥ 8. Thus n has 7 digits and the maximum sum of factorials of digits for a 7 digit number is 9!7 = 2,540,160, which is the upper bound.</p>
<p>Afterwards, we learned 50,000 worked just fine.</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: #b1b100;">my</span> <span style="color: #0000ff;">@fact</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: #cc66cc;">1</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">6</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">24</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">120</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">720</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">5</span>_040<span style="color: #339933;">,</span> <span style="color: #cc66cc;">40</span>_320<span style="color: #339933;">,</span> <span style="color: #cc66cc;">362</span>_880<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;"># 0 - 9 factorials </span>
<span style="color: #b1b100;">my</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: #b1b100;">for</span> <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$n</span> <span style="color: #009900;">&#40;</span>10<span style="color: #339933;">..</span>50_000<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #0000ff;">$x</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
  <span style="color: #0000ff;">$x</span><span style="color: #339933;">+=</span><span style="color: #0000ff;">$fact</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">$_</span><span style="color: #009900;">&#93;</span> <span style="color: #b1b100;">for</span> <span style="color: #000066;">split</span> <span style="color: #339933;">//,</span><span style="color: #0000ff;">$n</span><span style="color: #339933;">;</span>
  <span style="color: #0000ff;">$s</span><span style="color: #339933;">+=</span><span style="color: #0000ff;">$n</span> <span style="color: #b1b100;">if</span> <span style="color: #0000ff;">$n</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">$x</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 PE34 = $s&quot;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Runs < 1 second in Python.</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">fact = <span style="color: black;">&#40;</span><span style="color: #ff4500;">1</span>, <span style="color: #ff4500;">1</span>, <span style="color: #ff4500;">2</span>, <span style="color: #ff4500;">6</span>, <span style="color: #ff4500;">24</span>, <span style="color: #ff4500;">120</span>, <span style="color: #ff4500;">720</span>, <span style="color: #ff4500;">5040</span>, <span style="color: #ff4500;">40320</span>, <span style="color: #ff4500;">362880</span><span style="color: black;">&#41;</span>
s = <span style="color: #ff4500;">0</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">for</span> n <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">range</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">10</span>, <span style="color: #ff4500;">50000</span><span style="color: black;">&#41;</span>:
  x = <span style="color: #008000;">sum</span><span style="color: black;">&#40;</span> fact<span style="color: black;">&#91;</span><span style="color: #008000;">int</span><span style="color: black;">&#40;</span>d<span style="color: black;">&#41;</span><span style="color: black;">&#93;</span> <span style="color: #ff7700;font-weight:bold;">for</span> d <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>n<span style="color: black;">&#41;</span> <span style="color: black;">&#41;</span>
  <span style="color: #ff7700;font-weight:bold;">if</span> n == x: s+=n
&nbsp;
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Answer to PE34 = &quot;</span>,s</pre></div></div>

<h4><u>Comment</u></h4>
<p>This is a finite set of numbers and therefore a brute force solution is completely acceptable.  As long as it runs under a minute no further optimization is necessary.</p>
<ul>
<li>We use underscores in numeric literals for readability</li>
<li>See <a href="http://blog.dreamshire.com/2009/04/08/project-euler-problem-74-solution/">Problem 74</a></li>
<li><em>Mathematica:</em> <tt>Sum[Boole[n == Tr[IntegerDigits[n]!]] n, {n, 3, 1*^5}]</tt></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://blog.dreamshire.com/2009/04/08/project-euler-problem-34-solution/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
