<?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; Pentagonal Numbers</title>
	<atom:link href="http://blog.dreamshire.com/tag/pentagonal-numbers/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 78 Solution</title>
		<link>http://blog.dreamshire.com/2009/04/19/project-euler-problem-78-solution/</link>
		<comments>http://blog.dreamshire.com/2009/04/19/project-euler-problem-78-solution/#comments</comments>
		<pubDate>Sun, 19 Apr 2009 21:27:49 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[Project Euler Solutions]]></category>
		<category><![CDATA[Solutions 70-79]]></category>
		<category><![CDATA[Large Integers]]></category>
		<category><![CDATA[Pentagonal Numbers]]></category>
		<category><![CDATA[Perl]]></category>
		<category><![CDATA[Project Euler]]></category>
		<category><![CDATA[solution]]></category>

		<guid isPermaLink="false">http://blog.dreamshire.com/?p=158</guid>
		<description><![CDATA[Investigating the number of ways in which coins can be separated into piles.]]></description>
			<content:encoded><![CDATA[<h4><u>Problem Description</u></h4>
<p>Let p(<em>n</em>) represent the number of different ways in which <em>n</em> coins can be separated into piles. For example, five coins can separated into piles in exactly seven different ways, so <em>p</em>(5)=7.<br />
<center><br />
OOOOO<br />
OOOO   O<br />
OOO   OO<br />
OOO   O   O<br />
OO   OO   O<br />
OO   O   O   O<br />
O   O   O   O   O<br />
</center><br />
Find the least value of <em>n</em> for which p(<em>n</em>) is evenly divisible by one million.</p>
<h4><u>Analysis</u></h4>
<p>This problem is really asking to find the first term in the sequence of integer partitions that&#8217;s divisible by 1,000,000.  </p>
<p>A partition of an integer, n, is one way of describing how many ways the sum of positive integers, &le; n, can be added together to equal n, regardless of order.  The function p(n) is used to denote the number of partitions for n.  Below we show our 5 &#8220;coins&#8221; as addends to evaluate 7 partitions, that is p(5)=7.<br />
<center><tt>5 = 5<br />
  = 4+1<br />
  = 3+2<br />
  = 3+1+1<br />
  = 2+2+1<br />
  = 2+1+1+1<br />
  = 1+1+1+1+1</tt></center><br />
We use a <a href="http://en.wikipedia.org/wiki/Integer_partition#Generating_function">generating function</a> to create the series until we find the required <em>n</em>.<br />
The generating function requires at most 500 so-called generalized pentagonal numbers, given by <em>n</em>(3<em>n</em> &#8211; 1)/2 with 0, &plusmn; 1, &plusmn; 2, &plusmn; 3&#8230;, the first few of which are 0, 1, 2, 5, 7, 12, 15, 22, 26, 35, &#8230; (Sloane&#8217;s <a href="http://www.research.att.com/~njas/sequences/A001318">A001318</a>). </p>
<p>We have the following generating function which uses our pentagonal numbers as exponents:<br />
<img src="http://mathworld.wolfram.com/images/equations/PartitionFunctionP/Inline40.gif" alt="generating function" /></p>
<h4><u>Solution</u></h4>
<p>Runs < 10 seconds in Perl.</p>

<div class="wp_syntax"><div class="code"><pre class="perl" style="font-family:monospace;"><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$p</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: #0000ff;">$n</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$i</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$m</span><span style="color: #009900;">&#41;</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;">0</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span> 1e6<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">for</span> <span style="color: #0000ff;">$i</span> <span style="color: #009900;">&#40;</span>1<span style="color: #339933;">..</span>250<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>             <span style="color: #666666; font-style: italic;">#generate pentagonal numbers for generating function</span>
  <span style="color: #0000ff;">$p</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$i</span><span style="color: #339933;">*</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">3</span> <span style="color: #339933;">*</span> <span style="color: #0000ff;">$i</span> <span style="color: #339933;">-</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">/</span><span style="color: #cc66cc;">2</span><span style="color: #339933;">;</span>
  <span style="color: #000066;">push</span> <span style="color: #0000ff;">@k</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$p</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$p</span><span style="color: #339933;">+</span><span style="color: #0000ff;">$i</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$p</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">$n</span><span style="color: #339933;">++</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>         <span style="color: #666666; font-style: italic;">#expand generating function to calculate p(n)</span>
  <span style="color: #b1b100;">my</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$p</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$i</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #0000ff;">$p</span> <span style="color: #339933;">+=</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$i</span><span style="color: #339933;">%</span>4<span style="color: #339933;">&gt;</span><span style="color: #cc66cc;">1</span> <span style="color: #339933;">?</span> <span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span> <span style="color: #339933;">:</span> <span style="color: #cc66cc;">1</span> <span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> <span style="color: #0000ff;">$p</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">$n</span> <span style="color: #339933;">-</span> <span style="color: #0000ff;">$k</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">$i</span><span style="color: #339933;">++</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#93;</span> <span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$k</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">$i</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">&lt;=</span> <span style="color: #0000ff;">$n</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
  <span style="color: #0000ff;">$p</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">$n</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$p</span> <span style="color: #339933;">%</span> <span style="color: #0000ff;">$m</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 PE78 = &quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">$n</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span></pre></div></div>

<h4><u>Comments</u></h4>
<p>The full answer is:<br />
363253009254357859308323315773967616467<br />
158361736338932270710864607092686080534<br />
895417314045435376684389911706807452721<br />
591544937406153858232021581676352762505<br />
545553421158554245989201590354130448112<br />
450821973350979535709118842524107301749<br />
07784762924663654000000</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.dreamshire.com/2009/04/19/project-euler-problem-78-solution/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Project Euler Problem 44 Solution</title>
		<link>http://blog.dreamshire.com/2009/04/17/project-euler-problem-44-solution/</link>
		<comments>http://blog.dreamshire.com/2009/04/17/project-euler-problem-44-solution/#comments</comments>
		<pubDate>Sat, 18 Apr 2009 04:45:52 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[Project Euler Solutions]]></category>
		<category><![CDATA[Solutions 40-49]]></category>
		<category><![CDATA[Pentagonal Numbers]]></category>
		<category><![CDATA[Project Euler]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[solution]]></category>

		<guid isPermaLink="false">http://blog.dreamshire.com/?p=731</guid>
		<description><![CDATA[Find the smallest pair of pentagonal numbers whose sum and difference is pentagonal.]]></description>
			<content:encoded><![CDATA[<h4><u>Problem Description</u></h4>
<p>Pentagonal numbers are generated by the formula, P<sub><var>n</var></sub>=<var>n</var>(3<var>n</var>&minus;1)/2. The first ten pentagonal numbers are:</p>
<p style="text-align:center;">1, 5, 12, 22, 35, 51, 70, 92, 117, 145, &#8230;</p>
<p>It can be seen that P<sub>4</sub> + P<sub>7</sub> = 22 + 70 = 92 = P<sub>8</sub>. However, their difference, 70 &minus; 22 = 48, is not pentagonal.</p>
<p>Find the pair of pentagonal numbers, P<sub><var>j</var></sub> and P<sub><var>k</var></sub>, for which their sum and difference is pentagonal and D = |P<sub><var>k</var></sub> &minus; P<sub><var>j</var></sub>| is minimised; what is the value of D?</p>
<h4><u>Analysis</u></h4>
<p>How many pentagonal numbers are required to search through to solve the problem?  We wrote the following program and started with 10,000 pentagonal numbers and, after finding the correct answer, adjusted the requirement down to 2,400.  When the problem asks for D = |P<sub><var>k</var></sub> &minus; P<sub><var>j</var></sub>| to be minimized it simply means the smallest pair which, by our method, is the first occurrence found.</p>
<p>A set (hashable) is used to both hold the pentagonal numbers and to check if a number is pentagonal.  All that&#8217;s left is to permute the pairs and check that the difference and sum are pentagonal numbers.<br />
Runs < 2 seconds in Python.</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">pent = <span style="color: #008000;">set</span><span style="color: black;">&#40;</span> n <span style="color: #66cc66;">*</span> <span style="color: black;">&#40;</span><span style="color: #ff4500;">3</span><span style="color: #66cc66;">*</span>n - <span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span> / <span style="color: #ff4500;">2</span> <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;">2</span>,<span style="color: #ff4500;">2400</span><span style="color: black;">&#41;</span> <span style="color: black;">&#41;</span> 
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> pe44<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
  <span style="color: #ff7700;font-weight:bold;">for</span> pj <span style="color: #ff7700;font-weight:bold;">in</span> pent: 
    <span style="color: #ff7700;font-weight:bold;">for</span> pk <span style="color: #ff7700;font-weight:bold;">in</span> pent: 
      <span style="color: #ff7700;font-weight:bold;">if</span> pj - pk <span style="color: #ff7700;font-weight:bold;">in</span> pent <span style="color: #ff7700;font-weight:bold;">and</span> pj + pk <span style="color: #ff7700;font-weight:bold;">in</span> pent: 
        <span style="color: #ff7700;font-weight:bold;">return</span> pj - pk
&nbsp;
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Answer to PE44 = &quot;</span>,pe44<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre></div></div>

<p>So, a more challenging program might be to solve the problem without having to guess at how many pentagonal numbers are required.  That would require a function to determine if a number is pentagonal. (When the equation (&radic;(1 + 24 &middot; <em>n</em>) + 1) / 6 evaluates to an integer, <em>n</em>, is a pentagonal number.)</p>
<p>It turned out to be an interesting discovery and took longer to find an answer.  But, as luck would have it, was very helpful for solving problem 45.</p>
<h4><u>Solution</u></h4>
<p>Runs < 6 seconds 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> <span style="color: #dc143c;">math</span> <span style="color: #ff7700;font-weight:bold;">import</span> sqrt
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> is_pentagonal<span style="color: black;">&#40;</span>n<span style="color: black;">&#41;</span>:
  p = <span style="color: black;">&#40;</span> sqrt<span style="color: black;">&#40;</span><span style="color: #ff4500;">1</span> + <span style="color: #ff4500;">24</span><span style="color: #66cc66;">*</span>n<span style="color: black;">&#41;</span> + <span style="color: #ff4500;">1</span> <span style="color: black;">&#41;</span> / <span style="color: #ff4500;">6</span>
  <span style="color: #ff7700;font-weight:bold;">return</span> p==<span style="color: #008000;">int</span><span style="color: black;">&#40;</span>p<span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> pe44<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
  i=<span style="color: #ff4500;">0</span><span style="color: #66cc66;">;</span>
  pent = <span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span>
  <span style="color: #ff7700;font-weight:bold;">while</span> <span style="color: #008000;">True</span>:
    i+=<span style="color: #ff4500;">1</span>
    pent.<span style="color: black;">append</span><span style="color: black;">&#40;</span> i <span style="color: #66cc66;">*</span> <span style="color: black;">&#40;</span><span style="color: #ff4500;">3</span><span style="color: #66cc66;">*</span>i - <span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span> / <span style="color: #ff4500;">2</span> <span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">for</span> j <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>, <span style="color: #008000;">len</span><span style="color: black;">&#40;</span>pent<span style="color: black;">&#41;</span>-<span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>:
      <span style="color: #ff7700;font-weight:bold;">if</span> is_pentagonal<span style="color: black;">&#40;</span>pent<span style="color: black;">&#91;</span>i<span style="color: black;">&#93;</span>-pent<span style="color: black;">&#91;</span>j<span style="color: black;">&#93;</span><span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">and</span> is_pentagonal<span style="color: black;">&#40;</span>pent<span style="color: black;">&#91;</span>i<span style="color: black;">&#93;</span>+pent<span style="color: black;">&#91;</span>j<span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">return</span> pent<span style="color: black;">&#91;</span>i<span style="color: black;">&#93;</span>-pent<span style="color: black;">&#91;</span>j<span style="color: black;">&#93;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Answer to PE44 = &quot;</span>,pe44<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre></div></div>

<h4><u>Comments</u></h4>
<p>See <a href="http://blog.dreamshire.com/2009/04/10/project-euler-problem-45-solution/">Problem 45</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.dreamshire.com/2009/04/17/project-euler-problem-44-solution/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Project Euler Problem 45 Solution</title>
		<link>http://blog.dreamshire.com/2009/04/10/project-euler-problem-45-solution/</link>
		<comments>http://blog.dreamshire.com/2009/04/10/project-euler-problem-45-solution/#comments</comments>
		<pubDate>Fri, 10 Apr 2009 19:00:18 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[Project Euler Solutions]]></category>
		<category><![CDATA[Solutions 40-49]]></category>
		<category><![CDATA[Pentagonal Numbers]]></category>
		<category><![CDATA[Perl]]></category>
		<category><![CDATA[Project Euler]]></category>
		<category><![CDATA[solution]]></category>

		<guid isPermaLink="false">http://blog.dreamshire.com/?p=551</guid>
		<description><![CDATA[After 40755, what is the next triangle number that is also pentagonal and hexagonal?]]></description>
			<content:encoded><![CDATA[<h4><u>Problem Description</u></h4>
<p>Triangle, pentagonal, and hexagonal numbers are generated by the following formulae:</p>
<table>
<tr>
<td>Triangle</td>
<td>&nbsp;</td>
<td>T<sub><i>n</i></sub>=<i>n</i>(<i>n</i>+1)/2</td>
<td>&nbsp;</td>
<td>1, 3, 6, 10, 15, &#8230;</td>
</tr>
<tr>
<td>Pentagonal</td>
<td>&nbsp;</td>
<td>P<sub><i>n</i></sub>=<i>n</i>(3<i>n</i>&minus;1)/2</td>
<td>&nbsp;</td>
<td>1, 5, 12, 22, 35, &#8230;</td>
</tr>
<tr>
<td>Hexagonal</td>
<td>&nbsp;</td>
<td>H<sub><i>n</i></sub>=<i>n</i>(2<i>n</i>&minus;1)</td>
<td>&nbsp;</td>
<td>1, 6, 15, 28, 45, &#8230;</td>
</tr>
</table>
<p>It can be verified that T<sub>285</sub> = P<sub>165</sub> = H<sub>143</sub> = 40755.</p>
<p>Find the next triangle number that is also pentagonal and hexagonal.</p>
<h4><u>Analysis</u></h4>
<p>Note that Hexagonal numbers are a subset of Triangle numbers so we only determine the first occurrence of H<sub>i</sub> = P<sub>j</sub> to find our answer.  We can safely start our search from 166 as eluded to in the problem&#8217;s description.  </p>
<p>From our experience with problem 44 we were able to make our search trivial without the use of arrays.</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> <span style="color: #dc143c;">math</span> <span style="color: #ff7700;font-weight:bold;">import</span> sqrt
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> is_pentagonal<span style="color: black;">&#40;</span>n<span style="color: black;">&#41;</span>:
  p = <span style="color: black;">&#40;</span> sqrt<span style="color: black;">&#40;</span><span style="color: #ff4500;">1</span> + <span style="color: #ff4500;">24</span><span style="color: #66cc66;">*</span>n<span style="color: black;">&#41;</span> + <span style="color: #ff4500;">1</span> <span style="color: black;">&#41;</span> / <span style="color: #ff4500;">6</span>
  <span style="color: #ff7700;font-weight:bold;">return</span> p==<span style="color: #008000;">int</span><span style="color: black;">&#40;</span>p<span style="color: black;">&#41;</span>
&nbsp;
n = <span style="color: #ff4500;">165</span> 
<span style="color: #ff7700;font-weight:bold;">while</span> <span style="color: #008000;">True</span>:
  n += <span style="color: #ff4500;">1</span> 
  <span style="color: #008000;">hex</span> = n <span style="color: #66cc66;">*</span> <span style="color: black;">&#40;</span><span style="color: #ff4500;">2</span> <span style="color: #66cc66;">*</span> n-<span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>
  <span style="color: #ff7700;font-weight:bold;">if</span> is_pentagonal<span style="color: black;">&#40;</span><span style="color: #008000;">hex</span><span style="color: black;">&#41;</span>: <span style="color: #ff7700;font-weight:bold;">break</span> 
&nbsp;
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Answer to PE45 = &quot;</span>,<span style="color: #008000;">hex</span></pre></div></div>

<h4><u>Comments</u></h4>
<p>See <a href="http://blog.dreamshire.com/2009/04/17/project-euler-problem-44-solution/">Problem 44</a>.<br />
The next number in the series is 57722156241751, calculated in about 15 seconds.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.dreamshire.com/2009/04/10/project-euler-problem-45-solution/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
