<?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; Solutions 90-99</title>
	<atom:link href="http://blog.dreamshire.com/category/project-euler-solutions/project-euler-solutions-90-99/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 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 97 Solution</title>
		<link>http://blog.dreamshire.com/2009/03/25/project-euler-problem-97-solution/</link>
		<comments>http://blog.dreamshire.com/2009/03/25/project-euler-problem-97-solution/#comments</comments>
		<pubDate>Wed, 25 Mar 2009 18:33:18 +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[Perl]]></category>
		<category><![CDATA[Project Euler]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[solution]]></category>

		<guid isPermaLink="false">http://blog.dreamshire.com/?p=40</guid>
		<description><![CDATA[Find the last ten digits of the non-Mersenne prime: 28433 × 2<sup>7830457</sup> + 1.]]></description>
			<content:encoded><![CDATA[<h4><u>Problem Description</u></h4>
<p>The first known prime found to exceed one million digits was discovered in 1999, and is a Mersenne prime of the form 2<sup>6972593</sup>-1; it contains exactly 2,098,960 digits. Subsequently other Mersenne primes, of the form 2<sup>p</sup>-1, have been found which contain more digits.</p>
<p>However, in 2004 there was found a massive non-Mersenne prime which contains 2,357,207 digits: 28433×2<sup>7830457</sup>+1.<br />
Find the last ten digits of this prime number.</p>
<h4><u>Analysis</u></h4>
<p>We recognized we could reuse the understanding from <a href="http://blog.dreamshire.com/2009/03/24/project-euler-problem-48-solution/">Problem 48</a>.</p>
<h4><u>Solution</u></h4>
<p>Runs < 1 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;">def</span> PE97<span style="color: black;">&#40;</span>a, b, c, m<span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: black;">&#40;</span>c <span style="color: #66cc66;">*</span> <span style="color: #008000;">pow</span><span style="color: black;">&#40;</span>a, b, <span style="color: #ff4500;">10</span><span style="color: #66cc66;">**</span>m<span style="color: black;">&#41;</span> + <span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span> <span style="color: #66cc66;">%</span> <span style="color: #ff4500;">10</span><span style="color: #66cc66;">**</span>m
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Answer to PE97 = &quot;</span>,PE97<span style="color: black;">&#40;</span><span style="color: #ff4500;">2</span>, <span style="color: #ff4500;">7830457</span>, <span style="color: #ff4500;">28433</span>, <span style="color: #ff4500;">10</span><span style="color: black;">&#41;</span></pre></div></div>

<h4><u>Comments</u></h4>
<ul>
<li>See also <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/04/26/project-euler-problem-188-solution/">problem 188</a>.</li>
<li>Perl allows underscores to be used in numeric literals for readability.</li>
<li>Don’t forget that leading zeros are not printed.</li>
</ul>
<p>You can reduce the number of iterations significantly by breaking out the multiplication as follows:</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;">$max_n</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">7</span>_830_457<span style="color: #339933;">;</span>
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$m</span> <span style="color: #339933;">=</span> 1e10<span style="color: #339933;">;</span>
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$nx</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #0000ff;">$nx</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$nx</span> <span style="color: #339933;">*</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">**</span><span style="color: #cc66cc;">100</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">%</span> <span style="color: #0000ff;">$m</span> <span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span>1<span style="color: #339933;">..</span><span style="color: #0000ff;">$max_n</span><span style="color: #339933;">/</span><span style="color: #cc66cc;">100</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #0000ff;">$nx</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$nx</span> <span style="color: #339933;">*</span> <span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">%</span> <span style="color: #0000ff;">$m</span> <span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span>1<span style="color: #339933;">..</span><span style="color: #0000ff;">$max_n</span><span style="color: #339933;">%</span>100<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000066;">print</span> <span style="color: #ff0000;">&quot;Answer to PE97 = &quot;</span><span style="color: #339933;">,</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$nx</span> <span style="color: #339933;">*</span> <span style="color: #cc66cc;">28</span>_433 <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;">$m</span><span style="color: #339933;">,</span><span style="color: #ff0000;">&quot;n&quot;</span><span style="color: #339933;">;</span></pre></div></div>

<p>This version runs in a fraction of a second in Perl.  </p>
]]></content:encoded>
			<wfw:commentRss>http://blog.dreamshire.com/2009/03/25/project-euler-problem-97-solution/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
