<?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>therning.org/ magnus &#187; google</title>
	<atom:link href="http://therning.org/magnus/archives/tag/google/feed" rel="self" type="application/rss+xml" />
	<link>http://therning.org/magnus</link>
	<description>Incoherent mumblings</description>
	<lastBuildDate>Wed, 17 Mar 2010 00:39:19 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Google Treasure Hunt primes question</title>
		<link>http://therning.org/magnus/archives/353</link>
		<comments>http://therning.org/magnus/archives/353#comments</comments>
		<pubDate>Sat, 07 Jun 2008 22:58:06 +0000</pubDate>
		<dc:creator>Magnus</dc:creator>
				<category><![CDATA[Posts]]></category>
		<category><![CDATA[google]]></category>
		<category><![CDATA[haskell]]></category>
		<category><![CDATA[treasure hunt]]></category>

		<guid isPermaLink="false">http://therning.org/magnus/?p=353</guid>
		<description><![CDATA[In the comments to my post on the Google treasure hunt Andre asked me to put up my solution for the fourth problem.  The problems were personalised and here is mine:


  Find the smallest number that can be expressed as
  the sum of 3 consecutive prime numbers,
  the sum of 29 [...]]]></description>
			<content:encoded><![CDATA[<p>In the comments to my <a href="http://therning.org/magnus/archives/352">post on the Google treasure hunt</a> Andre asked me to put up my solution for the fourth problem.  The problems were personalised and here is mine:</p>

<blockquote>
  <p>Find the smallest number that can be expressed as<br />
  the sum of 3 consecutive prime numbers,<br />
  the sum of 29 consecutive prime numbers,<br />
  the sum of 373 consecutive prime numbers,<br />
  the sum of 665 consecutive prime numbers,<br />
  and is itself a prime number.</p>
</blockquote>

<p>The idea behind my solution is that if I have four lists of the sums, and a list of primes, then the solution to the problem is the smallest member of the intersection of all five lists.</p>

<p>I saved the problem of finding primes to the last and instead concentrated on summing up lists of integers in a convenient way.  I ended up with the following solution to that:</p>


<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;">sumN <span style="color: #339933; font-weight: bold;">::</span> <span style="color: #cccc00; font-weight: bold;">Int</span> <span style="color: #339933; font-weight: bold;">-&gt;</span> <span style="color: green;">&#91;</span><span style="color: #cccc00; font-weight: bold;">Integer</span><span style="color: green;">&#93;</span>
sumN n <span style="color: #339933; font-weight: bold;">=</span> <span style="font-weight: bold;">map</span> <span style="font-weight: bold;">fst</span> sumi
    <span style="color: #06c; font-weight: bold;">where</span>
        sumi <span style="color: #339933; font-weight: bold;">=</span> <span style="font-weight: bold;">tail</span> <span style="color: #339933; font-weight: bold;">$</span> <span style="font-weight: bold;">iterate</span> <span style="color: green;">&#40;</span>sumn<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span><span style="color: red;">0</span><span style="color: #339933; font-weight: bold;">,</span> primes<span style="color: green;">&#41;</span>
        sumn <span style="color: green;">&#40;</span>r<span style="color: #339933; font-weight: bold;">,</span> xs<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">=</span> <span style="color: green;">&#40;</span><span style="font-weight: bold;">sum</span> <span style="color: #339933; font-weight: bold;">$</span> <span style="font-weight: bold;">take</span> n xs<span style="color: #339933; font-weight: bold;">,</span> <span style="font-weight: bold;">tail</span> xs<span style="color: green;">&#41;</span></pre></div></div>


<p>The real work happens in the inner functions.  <code>sumn</code> picks out as many numbers as I want and sums them up, the reason for taking and returning a tuple is that <code>sumi</code> can use <code>iterate</code> to calculate the sequence of sums.  The first item in the result of <code>iterate a</code> is <code>a</code>, so <code>sumi</code> drops the first one.  This lets me create the needed lists of sums:</p>


<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;">sum3 <span style="color: #339933; font-weight: bold;">=</span> sumN <span style="color: red;">3</span>
sum29 <span style="color: #339933; font-weight: bold;">=</span> sumN <span style="color: red;">29</span>
sum373 <span style="color: #339933; font-weight: bold;">=</span> sumN <span style="color: red;">373</span>
sum665 <span style="color: #339933; font-weight: bold;">=</span> sumN <span style="color: red;">665</span></pre></div></div>


<p>After having a quick look at the implementation of <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Data-List.html#v%3Aintersect">Data.List.intersect</a> I realised it probably would be a bit too slow.  Instead I wrote one that exploits that all the involved lists are ordered:</p>


<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;">intersection a<span style="color: #339933; font-weight: bold;">@</span><span style="color: green;">&#40;</span>x:xs<span style="color: green;">&#41;</span> b<span style="color: #339933; font-weight: bold;">@</span><span style="color: green;">&#40;</span>y:ys<span style="color: green;">&#41;</span>
    <span style="color: #339933; font-weight: bold;">|</span> x <span style="color: #339933; font-weight: bold;">==</span> y <span style="color: #339933; font-weight: bold;">=</span> x : intersection xs ys
    <span style="color: #339933; font-weight: bold;">|</span> x <span style="color: #339933; font-weight: bold;">&lt;</span> y <span style="color: #339933; font-weight: bold;">=</span> intersection <span style="color: green;">&#40;</span><span style="font-weight: bold;">dropWhile</span> <span style="color: green;">&#40;</span><span style="color: #339933; font-weight: bold;">&lt;</span> y<span style="color: green;">&#41;</span> xs<span style="color: green;">&#41;</span> b
    <span style="color: #339933; font-weight: bold;">|</span> x <span style="color: #339933; font-weight: bold;">&gt;</span> y <span style="color: #339933; font-weight: bold;">=</span> intersection a <span style="color: green;">&#40;</span><span style="font-weight: bold;">dropWhile</span> <span style="color: green;">&#40;</span>x <span style="color: #339933; font-weight: bold;">&gt;</span><span style="color: green;">&#41;</span> ys<span style="color: green;">&#41;</span></pre></div></div>


<p>After convincing myself that this code did what I wanted (basically through playing in GHCi using an incorrect definition of primes, <code>primes = [1..]</code>) I turned to the problem of finding the primes.  Of course someone else had already written the <a href="http://en.literateprograms.org/Sieve_of_Eratosthenes_(Haskell)">Haskell code to find primes</a>.  That code fitted perfectly with mine since it produces an infinate list of <code>Integer</code>s.  Brilliant! <img src='http://therning.org/magnus/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>

<p>Finally here&#8217;s the <code>main</code> that allows me to compile the code and get the answer quickly (it&#8217;s slightly modified for the medium, basically I wrote it all on one, long line):</p>


<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;">main <span style="color: #339933; font-weight: bold;">::</span> <span style="color: #cccc00; font-weight: bold;">IO</span> <span style="color: green;">&#40;</span><span style="color: green;">&#41;</span>
main <span style="color: #339933; font-weight: bold;">=</span> <span style="color: #06c; font-weight: bold;">let</span>
        i1 <span style="color: #339933; font-weight: bold;">=</span> <span style="color: green;">&#40;</span>primes `intersection` sum665<span style="color: green;">&#41;</span>
        i2 <span style="color: #339933; font-weight: bold;">=</span> <span style="color: green;">&#40;</span>sum3 `intersection` sum373<span style="color: green;">&#41;</span>
        s <span style="color: #339933; font-weight: bold;">=</span>  i1 `intersection` i2 `intersection` sum29
    <span style="color: #06c; font-weight: bold;">in</span>
        <span style="font-weight: bold;">print</span> <span style="color: #339933; font-weight: bold;">$</span> <span style="font-weight: bold;">head</span> s</pre></div></div>


<p>Compiled, it runs in about 5.5 seconds on my desktop machine.</p>
<div class='bookmarkify'><a name='bookmarkify'></a><div class='linkbuttons'><a href='http://digg.com/submit?phase=2&amp;url=http://therning.org/magnus/archives/353&amp;title=Google Treasure Hunt primes question' title='Digg It!' onclick='target="_blank";' rel='nofollow'><img src='http://therning.org/magnus/wp-content/plugins/bookmarkify/digg.png' style='width:16px; height:16px;' alt='[Digg] ' /></a> <a href='http://reddit.com/submit?url=http://therning.org/magnus/archives/353&amp;title=Google Treasure Hunt primes question' title='Reddit' onclick='target="_blank";' rel='nofollow'><img src='http://therning.org/magnus/wp-content/plugins/bookmarkify/reddit.png' style='width:16px; height:16px;' alt='[Reddit] ' /></a>  <a title='See more bookmark and sharing options...' href='http://therning.org/magnus/archives/353#bookmarkify' rel='nofollow'><small>More&nbsp;&raquo;</small></a></div></div>]]></content:encoded>
			<wfw:commentRss>http://therning.org/magnus/archives/353/feed</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>Google Treasure Hunt, check!</title>
		<link>http://therning.org/magnus/archives/352</link>
		<comments>http://therning.org/magnus/archives/352#comments</comments>
		<pubDate>Wed, 04 Jun 2008 22:30:00 +0000</pubDate>
		<dc:creator>Magnus</dc:creator>
				<category><![CDATA[Posts]]></category>
		<category><![CDATA[google]]></category>
		<category><![CDATA[haskell]]></category>
		<category><![CDATA[treasure hunt]]></category>

		<guid isPermaLink="false">http://therning.org/magnus/?p=352</guid>
		<description><![CDATA[I personally found the first one to be the trickiest, quite a bit of thinking with a colleague before we saw Pascal&#8217;s triangle in the problem.  I was a bit worried that things would get trickier from there on.  It surely didn&#8217;t.

The second, summing certain lines taken from files matching certain glob patterns, [...]]]></description>
			<content:encoded><![CDATA[<p>I personally found the first one to be the trickiest, quite a bit of thinking with a colleague before we saw Pascal&#8217;s triangle in the problem.  I was a bit worried that things would get trickier from there on.  It surely didn&#8217;t.</p>

<p>The second, summing certain lines taken from files matching certain glob patterns, was easily solved with a short Haskell program.</p>

<p>The third, a routing problem was probably the easiest and I think most easily solved on paper.  I did make a mistake though, which I caught before typing my answer into the webform.</p>

<p>The fourth one, adding sequences of primes, was fairly straight forward.  I think it would have presented a bit more trouble if I didn&#8217;t make use of Haskell&#8217;s laziness and support for infinate lists though.</p>
<div class='bookmarkify'><a name='bookmarkify'></a><div class='linkbuttons'><a href='http://digg.com/submit?phase=2&amp;url=http://therning.org/magnus/archives/352&amp;title=Google Treasure Hunt, check!' title='Digg It!' onclick='target="_blank";' rel='nofollow'><img src='http://therning.org/magnus/wp-content/plugins/bookmarkify/digg.png' style='width:16px; height:16px;' alt='[Digg] ' /></a> <a href='http://reddit.com/submit?url=http://therning.org/magnus/archives/352&amp;title=Google Treasure Hunt, check!' title='Reddit' onclick='target="_blank";' rel='nofollow'><img src='http://therning.org/magnus/wp-content/plugins/bookmarkify/reddit.png' style='width:16px; height:16px;' alt='[Reddit] ' /></a>  <a title='See more bookmark and sharing options...' href='http://therning.org/magnus/archives/352#bookmarkify' rel='nofollow'><small>More&nbsp;&raquo;</small></a></div></div>]]></content:encoded>
			<wfw:commentRss>http://therning.org/magnus/archives/352/feed</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
		<item>
		<title>Replacing Google with Scroogle</title>
		<link>http://therning.org/magnus/archives/205</link>
		<comments>http://therning.org/magnus/archives/205#comments</comments>
		<pubDate>Fri, 06 Oct 2006 11:18:34 +0000</pubDate>
		<dc:creator>Magnus</dc:creator>
				<category><![CDATA[Posts]]></category>
		<category><![CDATA[autopost]]></category>
		<category><![CDATA[google]]></category>
		<category><![CDATA[scroogle]]></category>

		<guid isPermaLink="false">http://therning.org/magnus/archives/205</guid>
		<description><![CDATA[I&#8217;m not entirely convinced about Google&#8217;s evilness but I spent some time checking out Scroogle anyway. It is a bit difficult to use Scroogle as the default search engine in a browser since it uses POST requests in the web form. However, I found AutoPOST (yes, I used Scroogle to find it) and now I [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m not entirely convinced about Google&#8217;s evilness but I spent some time checking out <a href="http://www.scroogle.org/">Scroogle</a> anyway. It is a bit difficult to use Scroogle as the default search engine in a browser since it uses POST requests in the web form. However, I found <a href="http://www.io.com/~jsm/autopost/">AutoPOST</a> (yes, I used Scroogle to find it) and now I have the following bookmark in Epiphany:</p>

<p>http://www.io.com/~jsm/nph-ap.cgi/http://www.scroogle.org/cgi-bin/nbbw.cgi?Gw=%s&amp;n=2</p>

<p>Now, some things to ponder for the paranoid out there:</p>

<ul>
<li>Is Scroogle not evil? Does it log your IP?</li>
<li>What logging does AutoPOST do?</li>
</ul>

<p><em>[Edited 06-10-2006 17:46 BST]</em>
It turns out Scroogle already offers <a href="http://www.scroogle.org/scget.html">this</a>.</p>
<div class='bookmarkify'><a name='bookmarkify'></a><div class='linkbuttons'><a href='http://digg.com/submit?phase=2&amp;url=http://therning.org/magnus/archives/205&amp;title=Replacing Google with Scroogle' title='Digg It!' onclick='target="_blank";' rel='nofollow'><img src='http://therning.org/magnus/wp-content/plugins/bookmarkify/digg.png' style='width:16px; height:16px;' alt='[Digg] ' /></a> <a href='http://reddit.com/submit?url=http://therning.org/magnus/archives/205&amp;title=Replacing Google with Scroogle' title='Reddit' onclick='target="_blank";' rel='nofollow'><img src='http://therning.org/magnus/wp-content/plugins/bookmarkify/reddit.png' style='width:16px; height:16px;' alt='[Reddit] ' /></a>  <a title='See more bookmark and sharing options...' href='http://therning.org/magnus/archives/205#bookmarkify' rel='nofollow'><small>More&nbsp;&raquo;</small></a></div></div>]]></content:encoded>
			<wfw:commentRss>http://therning.org/magnus/archives/205/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Advanced Google</title>
		<link>http://therning.org/magnus/archives/38</link>
		<comments>http://therning.org/magnus/archives/38#comments</comments>
		<pubDate>Mon, 13 Jun 2005 09:01:50 +0000</pubDate>
		<dc:creator>Magnus</dc:creator>
				<category><![CDATA[Posts]]></category>
		<category><![CDATA[google]]></category>

		<guid isPermaLink="false">http://therning.org/magnus/?p=38</guid>
		<description><![CDATA[I&#8217;ve recently found del.icio.us and I&#8217;m reading
their RSS feed every now and then. Today I stumbled on this
gem. Basically
Google is not only a search engine, it&#8217;s also a


Calculator (2 * 2 + 2, 45% of 60)
List of definitions (define:google)
Unit converter (85 kg in lbs)
and much more&#8230;


Google is amazingly useful!
   More&#160;&#187;]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve recently found <a href="http://www.del.icio.us/">del.icio.us</a> and I&#8217;m reading
their RSS feed every now and then. Today I stumbled on <a href="http://www.googleguide.com/advanced_operators_reference.html">this
gem</a>. Basically
Google is not only a search engine, it&#8217;s also a</p>

<ul>
<li>Calculator (<code>2 * 2 + 2</code>, <code>45% of 60</code>)</li>
<li>List of definitions (<code>define:google</code>)</li>
<li>Unit converter (<code>85 kg in lbs</code>)</li>
<li>and much more&#8230;</li>
</ul>

<p>Google is amazingly useful!</p>
<div class='bookmarkify'><a name='bookmarkify'></a><div class='linkbuttons'><a href='http://digg.com/submit?phase=2&amp;url=http://therning.org/magnus/archives/38&amp;title=Advanced Google' title='Digg It!' onclick='target="_blank";' rel='nofollow'><img src='http://therning.org/magnus/wp-content/plugins/bookmarkify/digg.png' style='width:16px; height:16px;' alt='[Digg] ' /></a> <a href='http://reddit.com/submit?url=http://therning.org/magnus/archives/38&amp;title=Advanced Google' title='Reddit' onclick='target="_blank";' rel='nofollow'><img src='http://therning.org/magnus/wp-content/plugins/bookmarkify/reddit.png' style='width:16px; height:16px;' alt='[Reddit] ' /></a>  <a title='See more bookmark and sharing options...' href='http://therning.org/magnus/archives/38#bookmarkify' rel='nofollow'><small>More&nbsp;&raquo;</small></a></div></div>]]></content:encoded>
			<wfw:commentRss>http://therning.org/magnus/archives/38/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
