<?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; Large Integers</title>
	<atom:link href="http://blog.dreamshire.com/tag/large-integers/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 133 Solution</title>
		<link>http://blog.dreamshire.com/2009/06/16/project-euler-problem-133-solution/</link>
		<comments>http://blog.dreamshire.com/2009/06/16/project-euler-problem-133-solution/#comments</comments>
		<pubDate>Tue, 16 Jun 2009 07:49:39 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[Project Euler Solutions]]></category>
		<category><![CDATA[Solutions 100-149]]></category>
		<category><![CDATA[Large Integers]]></category>
		<category><![CDATA[Prime Numbers]]></category>
		<category><![CDATA[Project Euler]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Repunits]]></category>
		<category><![CDATA[solution]]></category>

		<guid isPermaLink="false">http://blog.dreamshire.com/?p=1654</guid>
		<description><![CDATA[Investigating which primes will never divide a repunit containing 10**n digits.]]></description>
			<content:encoded><![CDATA[<h4><u>Problem Description</u></h4>
<p>A number consisting entirely of ones is called a repunit. We shall define R(<i>k</i>) to be a repunit of length <i>k</i>; for example, R(6) = 111111.</p>
<p>Let us consider repunits of the form R(10<sup><i>n</i></sup>).</p>
<p>Although R(10), R(100), or R(1000) are not divisible by 17, R(10000) is divisible by 17. Yet there is no value of <i>n</i> for which R(10<sup><i>n</i></sup>) will divide by 19. In fact, it is remarkable that 11, 17, 41, and 73 are only four primes below one-hundred that can ever be a factor of R(10<sup><i>n</i></sup>).</p>
<p>Find the sum of all the primes below one-hundred thousand that will never be a factor of R(10<sup><i>n</i></sup>).</p>
<h4><u>Analysis</u></h4>
<p>This is a modified solution from problem 132.  Really all we&#8217;re doing is finding the prime factors of a very large R(n) using the Python modulus power function.  We sum only those primes which are not factors.</p>
<h4><u>Solution</u></h4>
<p>Runs < 3 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> Euler <span style="color: #ff7700;font-weight:bold;">import</span> is_prime
&nbsp;
limit, q, s = <span style="color: #ff4500;">100000</span>, <span style="color: #008000;">pow</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">10</span>, <span style="color: #ff4500;">50</span><span style="color: black;">&#41;</span>, <span style="color: #ff4500;">5</span>
<span style="color: #ff7700;font-weight:bold;">for</span> p <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">xrange</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">5</span>, limit, <span style="color: #ff4500;">2</span><span style="color: black;">&#41;</span>:
  <span style="color: #ff7700;font-weight:bold;">if</span> is_prime<span style="color: black;">&#40;</span>p<span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">and</span> <span style="color: #008000;">pow</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">10</span>, q, p<span style="color: black;">&#41;</span> <span style="color: #66cc66;">!</span>= <span style="color: #ff4500;">1</span>: s += p
&nbsp;
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Answer to PE133 = &quot;</span>, s</pre></div></div>

<h4><u>Comments</u></h4>
<p>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>.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.dreamshire.com/2009/06/16/project-euler-problem-133-solution/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Project Euler Problem 99 Solution</title>
		<link>http://blog.dreamshire.com/2009/05/28/project-euler-problem-99-solution/</link>
		<comments>http://blog.dreamshire.com/2009/05/28/project-euler-problem-99-solution/#comments</comments>
		<pubDate>Fri, 29 May 2009 04:18:39 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[Project Euler Solutions]]></category>
		<category><![CDATA[Solutions 90-99]]></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=1487</guid>
		<description><![CDATA[Which base/exponent pair in the file has the greatest numerical value?]]></description>
			<content:encoded><![CDATA[<h4><u>Problem Description</u></h4>
<p>Comparing two numbers written in index form like 2<sup>11</sup> and 3<sup>7</sup> is not difficult, as any calculator would confirm that 2<sup>11</sup> = 2048 &lt; 3<sup>7</sup> = 2187.</p>
<p>However, confirming that 632382<sup>518061</sup> &gt; 519432<sup>525806</sup> would be much more difficult, as both numbers contain over three million digits.</p>
<p>Using <a href='http://projecteuler.net/project/base_exp.txt'>base_exp.txt</a> (right click and &#8216;Save Link/Target As&#8230;&#8217;), a 22K text file containing one thousand lines with a base/exponent pair on each line, determine which line number has the greatest numerical value.</p>
<p class='info'>NOTE: The first two lines in the file represent the numbers in the example given above.</p>
<h4><u>Analysis</u></h4>
<p>Very simple to compare the product of the exponent by the log of the base and keep the maximum value and respective line number. </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> log
&nbsp;
mv, ml, ln = <span style="color: #ff4500;">0</span>, <span style="color: #ff4500;">0</span>, <span style="color: #ff4500;">0</span> 
<span style="color: #ff7700;font-weight:bold;">for</span> line <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">file</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'base_exp.txt'</span><span style="color: black;">&#41;</span>: 
  ln += <span style="color: #ff4500;">1</span>
  b, e = line.<span style="color: black;">split</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">','</span><span style="color: black;">&#41;</span> 
  v = <span style="color: #008000;">int</span><span style="color: black;">&#40;</span>e<span style="color: black;">&#41;</span> <span style="color: #66cc66;">*</span> log<span style="color: black;">&#40;</span><span style="color: #008000;">int</span><span style="color: black;">&#40;</span>b<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span> 
  <span style="color: #ff7700;font-weight:bold;">if</span> v <span style="color: #66cc66;">&gt;</span> mv: 
    mv, ml = v, ln 
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Answer to PE99 = &quot;</span>, ml</pre></div></div>

<h4><u>Comments</u></h4>
]]></content:encoded>
			<wfw:commentRss>http://blog.dreamshire.com/2009/05/28/project-euler-problem-99-solution/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<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 20 Solution</title>
		<link>http://blog.dreamshire.com/2009/05/18/project-euler-problem-20-solution/</link>
		<comments>http://blog.dreamshire.com/2009/05/18/project-euler-problem-20-solution/#comments</comments>
		<pubDate>Mon, 18 May 2009 19:48:37 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[Project Euler Solutions]]></category>
		<category><![CDATA[Solutions 20-29]]></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=1305</guid>
		<description><![CDATA[Find the sum of digits in 100!]]></description>
			<content:encoded><![CDATA[<h4><u>Problem Description</u></h4>
<p><i>n</i>! means <i>n</i> &times; (<i>n</i> &minus; 1) &times; &#8230; &times; 3 &times; 2 &times; 1</p>
<p>Find the sum of the digits in the number 100!</p>
<h4><u>Analysis</u></h4>
<p>Same solution as <a href="http://blog.dreamshire.com/2009/05/18/project-euler-problem-16-solution/">problem 16</a>.</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;
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Answer to PE20 = &quot;</span>, <span style="color: #008000;">sum</span><span style="color: black;">&#40;</span><span style="color: #008000;">int</span><span style="color: black;">&#40;</span>i<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;">str</span><span style="color: black;">&#40;</span>fact<span style="color: black;">&#40;</span><span style="color: #ff4500;">100</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span></pre></div></div>

<h4><u>Comments</u></h4>
]]></content:encoded>
			<wfw:commentRss>http://blog.dreamshire.com/2009/05/18/project-euler-problem-20-solution/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Project Euler Problem 16 Solution</title>
		<link>http://blog.dreamshire.com/2009/05/18/project-euler-problem-16-solution/</link>
		<comments>http://blog.dreamshire.com/2009/05/18/project-euler-problem-16-solution/#comments</comments>
		<pubDate>Mon, 18 May 2009 19:26:22 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[Project Euler Solutions]]></category>
		<category><![CDATA[Solutions 10-19]]></category>
		<category><![CDATA[Large Integers]]></category>
		<category><![CDATA[Project Euler]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[solution]]></category>
		<category><![CDATA[Strings]]></category>

		<guid isPermaLink="false">http://blog.dreamshire.com/?p=1299</guid>
		<description><![CDATA[What is the sum of the digits of the number 2^1000?]]></description>
			<content:encoded><![CDATA[<h4><u>Problem Description</u></h4>
<p>2<sup>15</sup> = 32768 and the sum of its digits is 3 + 2 + 7 + 6 + 8 = 26.</p>
<p>What is the sum of the digits of the number 2<sup>1000</sup>?</p>
<h4><u>Analysis</u></h4>
<p>Calculate 2<sup>1000</sup>, split the digits and add them together.</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;">print</span> <span style="color: #483d8b;">&quot;Answer to PE16 = &quot;</span>, <span style="color: #008000;">sum</span><span style="color: black;">&#40;</span><span style="color: #008000;">int</span><span style="color: black;">&#40;</span>i<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;">str</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">2</span><span style="color: #66cc66;">**</span><span style="color: #ff4500;">1000</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span></pre></div></div>

<h4><u>Comments</u></h4>
]]></content:encoded>
			<wfw:commentRss>http://blog.dreamshire.com/2009/05/18/project-euler-problem-16-solution/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Project Euler Problem 13 Solution</title>
		<link>http://blog.dreamshire.com/2009/05/18/project-euler-problem-13-solution/</link>
		<comments>http://blog.dreamshire.com/2009/05/18/project-euler-problem-13-solution/#comments</comments>
		<pubDate>Mon, 18 May 2009 18:08:33 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[Project Euler Solutions]]></category>
		<category><![CDATA[Solutions 10-19]]></category>
		<category><![CDATA[Large Integers]]></category>
		<category><![CDATA[Project Euler]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[solution]]></category>
		<category><![CDATA[Strings]]></category>

		<guid isPermaLink="false">http://blog.dreamshire.com/?p=1290</guid>
		<description><![CDATA[Find the first ten digits of the sum of one-hundred 50-digit numbers.]]></description>
			<content:encoded><![CDATA[<h4><u>Problem Description</u></h4>
<p>Work out the first ten digits of the sum of the following one-hundred 50-digit numbers.</p>
<div style='font-family:courier new;font-size:10pt;text-align:center;'>
37107287533902102798797998220837590246510135740250<br />
46376937677490009712648124896970078050417018260538<br />
&#8230; {data continues}
</div>
<h4><u>Analysis</u></h4>
<p>Only the first 11 digits of each of the 100 numbers are required for the first 10 digits of the sum.  The numbers were written to a file, pe13.txt, to save space.</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: #008000;">open</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'pe13.txt'</span><span style="color: black;">&#41;</span>.<span style="color: black;">read</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>.<span style="color: black;">split</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
total = <span style="color: #008000;">sum</span><span style="color: black;">&#40;</span> <span style="color: black;">&#91;</span><span style="color: #008000;">int</span><span style="color: black;">&#40;</span>x<span style="color: black;">&#91;</span>:<span style="color: #ff4500;">11</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">for</span> x <span style="color: #ff7700;font-weight:bold;">in</span> f<span style="color: black;">&#93;</span> <span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Answer to PE13 &quot;</span>, <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>total<span style="color: black;">&#41;</span><span style="color: black;">&#91;</span>:<span style="color: #ff4500;">10</span><span style="color: black;">&#93;</span></pre></div></div>

<h4><u>Comments</u></h4>
]]></content:encoded>
			<wfw:commentRss>http://blog.dreamshire.com/2009/05/18/project-euler-problem-13-solution/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Project Euler Problem 26 Solution</title>
		<link>http://blog.dreamshire.com/2009/04/30/project-euler-problem-26-solution/</link>
		<comments>http://blog.dreamshire.com/2009/04/30/project-euler-problem-26-solution/#comments</comments>
		<pubDate>Thu, 30 Apr 2009 22:23:48 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[Project Euler Solutions]]></category>
		<category><![CDATA[Solutions 20-29]]></category>
		<category><![CDATA[Common Routines]]></category>
		<category><![CDATA[Large Integers]]></category>
		<category><![CDATA[Prime Numbers]]></category>
		<category><![CDATA[Project Euler]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[solution]]></category>

		<guid isPermaLink="false">http://blog.dreamshire.com/?p=1041</guid>
		<description><![CDATA[Find the value of d < 1000 for which 1/d contains the longest recurring cycle.]]></description>
			<content:encoded><![CDATA[<h4><u>Problem Description</u></h4>
<p>A unit fraction contains 1 in the numerator. The decimal representation of the unit fractions with denominators 2 to 10 are given:</p>
<blockquote>
<table>
<tr>
<td><sup>1</sup>/<sub>2</sub></td>
<td>=&nbsp;</td>
<td>0.5</td>
</tr>
<tr>
<td><sup>1</sup>/<sub>3</sub></td>
<td>=&nbsp;</td>
<td>0.(3)</td>
</tr>
<tr>
<td><sup>1</sup>/<sub>4</sub></td>
<td>=&nbsp;</td>
<td>0.25</td>
</tr>
<tr>
<td><sup>1</sup>/<sub>5</sub></td>
<td>=&nbsp;</td>
<td>0.2</td>
</tr>
<tr>
<td><sup>1</sup>/<sub>6</sub></td>
<td>=&nbsp;</td>
<td>0.1(6)</td>
</tr>
<tr>
<td><sup>1</sup>/<sub>7</sub></td>
<td>=&nbsp;</td>
<td>0.(142857)</td>
</tr>
<tr>
<td><sup>1</sup>/<sub>8</sub></td>
<td>=&nbsp;</td>
<td>0.125</td>
</tr>
<tr>
<td><sup>1</sup>/<sub>9</sub></td>
<td>=&nbsp;</td>
<td>0.(1)</td>
</tr>
<tr>
<td><sup>1</sup>/<sub>10</sub></td>
<td>=&nbsp;</td>
<td>0.1</td>
</tr>
</table>
</blockquote>
<p>Where 0.1(6) means 0.166666&#8230;, and has a 1-digit recurring cycle. It can be seen that <sup>1</sup>/<sub>7</sub> has a 6-digit recurring cycle.</p>
<p>Find the value of <i>d</i> &lt; 1000 for which <sup>1</sup>/<sub><i>d</i></sub> contains the longest recurring cycle in its decimal fraction part.</p>
<h4><u>Analysis</u></h4>
<p>This is a useful application of <a href="http://en.wikipedia.org/wiki/Fermat's_little_theorem">Fermat&#8217;s little theorem</a> that says: 1/<em>d</em> has a cycle of n digits if 10<sup>n</sup>&minus;1 mod <em>d</em> = 0 for prime <em>d</em>.  It also follows that a prime number in the denominator, <em>d</em>,  can yield up to <em>d</em>-1 repeating decimal digits.</p>
<p>Since the problem wants the longest string of repeating digits from 1 &lt; <i>d</i> &lt; 1000 we started at the top and worked down calculating the length of the chain until we found one with d &minus; 1 digits.</p>
<p>Calculating a cycle length:<br />
d=7 ; c=1<br />
9 % 7 = 2 ; c=2<br />
99 % 7 = 1; c=3<br />
999 % 7 = 5; c=4<br />
9999 % 7 = 3; c=5<br />
99999 % 7 = 4; c=6<br />
999999 % 7 = 0; break</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> is_prime
&nbsp;
<span style="color: #ff7700;font-weight:bold;">for</span> d <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;">999</span>, <span style="color: #ff4500;">1</span>, -<span style="color: #ff4500;">2</span><span style="color: black;">&#41;</span>:
  <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #ff7700;font-weight:bold;">not</span> is_prime<span style="color: black;">&#40;</span>d<span style="color: black;">&#41;</span>: <span style="color: #ff7700;font-weight:bold;">continue</span>
  c = <span style="color: #ff4500;">1</span>
  <span style="color: #ff7700;font-weight:bold;">while</span> <span style="color: black;">&#40;</span><span style="color: #008000;">pow</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">10</span>, c<span style="color: black;">&#41;</span> - <span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span> <span style="color: #66cc66;">%</span> d <span style="color: #66cc66;">!</span>= <span style="color: #ff4500;">0</span>:
    c += <span style="color: #ff4500;">1</span>
  <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: black;">&#40;</span>d-c<span style="color: black;">&#41;</span> == <span style="color: #ff4500;">1</span>: <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 PE26 = &quot;</span>,d</pre></div></div>

<h4><u>Comments</u></h4>
<p>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>.<br />
for d < 2000: 1997<br />
for d < 2500: 2473<br />
for d < 10,000: 9967<br />
for d < 20,000: 19993<br />
for d < 25,000: 24989</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.dreamshire.com/2009/04/30/project-euler-problem-26-solution/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Project Euler Problem 188 Solution</title>
		<link>http://blog.dreamshire.com/2009/04/26/project-euler-problem-188-solution/</link>
		<comments>http://blog.dreamshire.com/2009/04/26/project-euler-problem-188-solution/#comments</comments>
		<pubDate>Mon, 27 Apr 2009 06:14:01 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[Project Euler Solutions]]></category>
		<category><![CDATA[Solutions 150-199]]></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=966</guid>
		<description><![CDATA[The hyperexponentiation of a number]]></description>
			<content:encoded><![CDATA[<h4><u>Problem Description</u></h4>
<p>The <span style="font-style: italic">hyperexponentiation</span> or <span style="font-style: italic">tetration</span> of a number a by a positive integer b, denoted by a&uarr;&uarr;b or <sup>b</sup>a, is recursively defined by:</p>
<p>a&uarr;&uarr;1 = a,<br />
a&uarr;&uarr;(k+1) = a<sup>(a&uarr;&uarr;k)</sup>.</p>
<p>
Thus we have e.g. 3&uarr;&uarr;2 = 3<sup>3</sup> = 27, hence 3&uarr;&uarr;3 = 3<sup>27</sup> = 7625597484987 and 3&uarr;&uarr;4 is roughly 10<sup>3.6383346400240996*10^12</sup>.</p>
<p>Find the last 8 digits of 1777&uarr;&uarr;1855.</p>
<h4><u>Analysis</u></h4>
<p>Using the little known optional third parameter of Python&#8217;s pow(x, y [,z]) function that provides x to the power y, modulo z (computed more efficiently than pow(x, y) % z) we were able to simply loop through the  hyperexponentiation of a&uarr;&uarr;b.</p>
<p>This is similar to <a href="http://blog.dreamshire.com/2009/03/25/project-euler-problem-97-solution/">problem 97</a>.</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> hyper_exp<span style="color: black;">&#40;</span>a, b, m<span style="color: black;">&#41;</span>:
    temp = <span style="color: #ff4500;">1</span>
    <span style="color: #ff7700;font-weight:bold;">while</span> b:
        temp = <span style="color: #008000;">pow</span><span style="color: black;">&#40;</span>a, temp, <span style="color: #ff4500;">10</span><span style="color: #66cc66;">**</span>m<span style="color: black;">&#41;</span>
        b -= <span style="color: #ff4500;">1</span>
    <span style="color: #ff7700;font-weight:bold;">return</span> temp
&nbsp;
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Answer to PE188 = &quot;</span>,hyper_exp<span style="color: black;">&#40;</span><span style="color: #ff4500;">1777</span>, <span style="color: #ff4500;">1855</span>, <span style="color: #ff4500;">8</span><span style="color: black;">&#41;</span></pre></div></div>

<h4><u>Comments</u></h4>
<p><strong>Note:</strong> The last eight digits remain constant by the eighth iteration.<br />
See <a href="http://blog.dreamshire.com/2009/03/24/project-euler-problem-48-solution/">problem 48</a> &#038; <a href="http://blog.dreamshire.com/2009/03/25/project-euler-problem-97-solution/">problem 97</a></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.dreamshire.com/2009/04/26/project-euler-problem-188-solution/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Project Euler Problem 119 Solution</title>
		<link>http://blog.dreamshire.com/2009/04/20/project-euler-problem-119-solution/</link>
		<comments>http://blog.dreamshire.com/2009/04/20/project-euler-problem-119-solution/#comments</comments>
		<pubDate>Tue, 21 Apr 2009 00:05:18 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[Project Euler Solutions]]></category>
		<category><![CDATA[Solutions 100-149]]></category>
		<category><![CDATA[Large Integers]]></category>
		<category><![CDATA[Project Euler]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Strings]]></category>

		<guid isPermaLink="false">http://blog.dreamshire.com/?p=797</guid>
		<description><![CDATA[Investigating the numbers which are equal to sum of their digits raised to some power.]]></description>
			<content:encoded><![CDATA[<h4><u>Problem Description</u></h4>
<p>The number 512 is interesting because it is equal to the sum of its digits raised to some power: 5 + 1 + 2 = 8, and 8<sup>3</sup> = 512. Another example of a number with this property is 614656 = 28<sup>4</sup>.</p>
<p>We shall define <i>a</i><sub>n</sub> to be the <i>n</i>th term of this sequence and insist that a number must contain at least two digits to have a sum.</p>
<p>You are given that <i>a</i><sub>2</sub> = 512 and <i>a</i><sub>10</sub> = 614656.</p>
<p>Find <i>a</i><sub>30</sub>.</p>
<h4><u>Analysis</u></h4>
<p>Taking advantage of the easy large integer support in Python, we iterated two loops representing the base and exponent with the intention of accommodating inquiries up to <em>a</em><sub>200</sub>.  Ignoring a<sup>b</sup> values < 10 it was a simple process of adding the digits of the powers and comparing that sum to the base.  After collecting relevant values into an array, it was sorted and the proper index printed for the answer.</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> sum_of_digits<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;">sum</span><span style="color: black;">&#40;</span><span style="color: #008000;">map</span><span style="color: black;">&#40;</span><span style="color: #008000;">int</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: black;">&#41;</span>
&nbsp;
a = <span style="color: black;">&#91;</span><span style="color: black;">&#93;</span>
n = <span style="color: #ff4500;">30</span>  <span style="color: #808080; font-style: italic;">#tested up to n = 220</span>
<span style="color: #ff7700;font-weight:bold;">for</span> b <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">xrange</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">2</span>, <span style="color: #ff4500;">600</span><span style="color: black;">&#41;</span>:
  <span style="color: #ff7700;font-weight:bold;">for</span> e <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">xrange</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">2</span>, <span style="color: #ff4500;">50</span><span style="color: black;">&#41;</span>:
     p = b<span style="color: #66cc66;">**</span>e
     <span style="color: #ff7700;font-weight:bold;">if</span> sum_of_digits<span style="color: black;">&#40;</span>p<span style="color: black;">&#41;</span> == b: a.<span style="color: black;">append</span><span style="color: black;">&#40;</span>p<span style="color: black;">&#41;</span><span style="color: #66cc66;">;</span> 
     <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #008000;">len</span><span style="color: black;">&#40;</span>a<span style="color: black;">&#41;</span><span style="color: #66cc66;">&gt;</span>n<span style="color: #66cc66;">*</span><span style="color: #ff4500;">1.1</span>: <span style="color: #ff7700;font-weight:bold;">break</span>
a.<span style="color: black;">sort</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Answer to PE119 = a(%d) =&quot;</span> <span style="color: #66cc66;">%</span> n, a<span style="color: black;">&#91;</span>n-<span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span></pre></div></div>

<h4><u>Comments</u></h4>
<p>The base index of arrays in Python begin with 0.  We need to subtract one from our index because the problem is using a base of 1; <em>a</em><sub>30</sub> = a[29].<br />
We provide an early escape when we have calculated enough values to provide the result for the given index.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.dreamshire.com/2009/04/20/project-euler-problem-119-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>
	</channel>
</rss>
