<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	>
<channel>
	<title>Comments on: Google Treasure Hunt primes question</title>
	<atom:link href="http://therning.org/magnus/archives/353/feed" rel="self" type="application/rss+xml" />
	<link>http://therning.org/magnus/archives/353</link>
	<description>Incoherent mumblings</description>
	<pubDate>Tue, 06 Jan 2009 15:23:46 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.6.3</generator>
		<item>
		<title>By: ciju</title>
		<link>http://therning.org/magnus/archives/353#comment-126608</link>
		<dc:creator>ciju</dc:creator>
		<pubDate>Tue, 10 Jun 2008 14:38:06 +0000</pubDate>
		<guid isPermaLink="false">http://therning.org/magnus/?p=353#comment-126608</guid>
		<description>&lt;p&gt;Here's another solution. Runs in around 0.2 seconds, although using C.
http://ciju.wordpress.com/2008/06/03/google-treasure-hunt/&lt;/p&gt;

&lt;p&gt;The method should work in haskell also. If you try it, please do let me know how much speedup u get, if any.&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>Here&#8217;s another solution. Runs in around 0.2 seconds, although using C.<br />
<a href="http://ciju.wordpress.com/2008/06/03/google-treasure-hunt/" rel="nofollow">http://ciju.wordpress.com/2008/06/03/google-treasure-hunt/</a></p>
<p>The method should work in haskell also. If you try it, please do let me know how much speedup u get, if any.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Nikolay</title>
		<link>http://therning.org/magnus/archives/353#comment-126556</link>
		<dc:creator>Nikolay</dc:creator>
		<pubDate>Tue, 10 Jun 2008 11:12:01 +0000</pubDate>
		<guid isPermaLink="false">http://therning.org/magnus/?p=353#comment-126556</guid>
		<description>&lt;p&gt;I've used such definition of sums with stream-fusion:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;sprimesS :: Int -&#62; Stream Integer
sprimesS n &#124; n == 1 = stream primes
                 &#124; n &#62; 1 = Stream next (L (sum h,primes,t))
    where
        (h,t) = splitAt n primes
        next (L (s,(x:xs),(y:ys))) = Yield s (L (s-x+y,xs,ys))

sprimes = unstream . sprimesS&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;That should prevent much GC, because all we need is primes list, all other is shifting sums.&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>I&#8217;ve used such definition of sums with stream-fusion:</p>
<pre><code>sprimesS :: Int -&gt; Stream Integer
sprimesS n | n == 1 = stream primes
                 | n &gt; 1 = Stream next (L (sum h,primes,t))
    where
        (h,t) = splitAt n primes
        next (L (s,(x:xs),(y:ys))) = Yield s (L (s-x+y,xs,ys))

sprimes = unstream . sprimesS</code></pre>
<p>That should prevent much GC, because all we need is primes list, all other is shifting sums.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Samuel</title>
		<link>http://therning.org/magnus/archives/353#comment-126299</link>
		<dc:creator>Samuel</dc:creator>
		<pubDate>Mon, 09 Jun 2008 09:45:45 +0000</pubDate>
		<guid isPermaLink="false">http://therning.org/magnus/?p=353#comment-126299</guid>
		<description>&lt;p&gt;Interesting how we all used pretty much the same solution. :)&lt;/p&gt;

&lt;p&gt;I used the following definition of sumN (equivalent to Russel's function). Should be much faster, since it doesn't need to iterate through 665 primes for each element.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;sumN n = sumN' primes (drop n primes) (sum $ take n primes)
sumN' (x:xs) (y:ys) accum = accum:sumN' xs ys (accum+y-x)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;My definition of primes, btw, is the following. Slower than the fastest one on the quoted page, but still pretty fast and elegant.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;primes = 2:[x &#124; x &#60;- [3,5..], is_prime x]
is_prime n = all (/= 0) [n `mod` x &#124; x &#60;- takeWhile ((&#60;=n).(^2)) primes]
&lt;/code&gt;&lt;/pre&gt;
</description>
		<content:encoded><![CDATA[<p>Interesting how we all used pretty much the same solution. <img src='http://therning.org/magnus/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>I used the following definition of sumN (equivalent to Russel&#8217;s function). Should be much faster, since it doesn&#8217;t need to iterate through 665 primes for each element.</p>
<pre><code>sumN n = sumN' primes (drop n primes) (sum $ take n primes)
sumN' (x:xs) (y:ys) accum = accum:sumN' xs ys (accum+y-x)
</code></pre>
<p>My definition of primes, btw, is the following. Slower than the fastest one on the quoted page, but still pretty fast and elegant.</p>
<pre><code>primes = 2:[x | x &lt;- [3,5..], is_prime x]
is_prime n = all (/= 0) [n `mod` x | x &lt;- takeWhile ((&lt;=n).(^2)) primes]
</code></pre>
]]></content:encoded>
	</item>
	<item>
		<title>By: Magnus</title>
		<link>http://therning.org/magnus/archives/353#comment-126283</link>
		<dc:creator>Magnus</dc:creator>
		<pubDate>Mon, 09 Jun 2008 07:37:23 +0000</pubDate>
		<guid isPermaLink="false">http://therning.org/magnus/?p=353#comment-126283</guid>
		<description>&lt;p&gt;@Russel,&lt;/p&gt;

&lt;p&gt;That is a beautiful definition of &lt;code&gt;sumN&lt;/code&gt;!  I wasn't aware of the existance of the &lt;code&gt;tails&lt;/code&gt; function before.&lt;/p&gt;

&lt;p&gt;@Peter,&lt;/p&gt;

&lt;p&gt;Thanks for pointing that out, it does make it a bit more beautiful and readable.&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>@Russel,</p>
<p>That is a beautiful definition of <code>sumN</code>!  I wasn&#8217;t aware of the existance of the <code>tails</code> function before.</p>
<p>@Peter,</p>
<p>Thanks for pointing that out, it does make it a bit more beautiful and readable.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Peter</title>
		<link>http://therning.org/magnus/archives/353#comment-126221</link>
		<dc:creator>Peter</dc:creator>
		<pubDate>Mon, 09 Jun 2008 00:53:25 +0000</pubDate>
		<guid isPermaLink="false">http://therning.org/magnus/?p=353#comment-126221</guid>
		<description>&lt;p&gt;Oops.  There's a typo in my previous comment.  It should be:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;main = print $ head $ foldl1 intersect [sumN n &#124; n &#60;- [1, 3, 29, 373, 665]]&lt;/code&gt;&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>Oops.  There&#8217;s a typo in my previous comment.  It should be:</p>
<p><code>main = print $ head $ foldl1 intersect [sumN n | n &lt;- [1, 3, 29, 373, 665]]</code></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Peter</title>
		<link>http://therning.org/magnus/archives/353#comment-126219</link>
		<dc:creator>Peter</dc:creator>
		<pubDate>Mon, 09 Jun 2008 00:50:58 +0000</pubDate>
		<guid isPermaLink="false">http://therning.org/magnus/?p=353#comment-126219</guid>
		<description>&lt;p&gt;Thanks for showing us what you did!&lt;/p&gt;

&lt;p&gt;I really like foobar's definition of sumN -- much better than the one I came up with -- although I'd have used zipWith, like this:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;sumN 1 = primes
sumN (n+1) = zipWith (+) primes $ tail $ sumN n&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;For extra prettiness, notice that primes == sumN 1 and that Andre's version of intersect can be derived from Magnus' version of intersect using foldl1:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;main = print $ head $ foldl1 intersect [sumN n primes &#124; n &#60;- [1, 3, 29, 373, 665]]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;:-)&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>Thanks for showing us what you did!</p>
<p>I really like foobar&#8217;s definition of sumN &#8212; much better than the one I came up with &#8212; although I&#8217;d have used zipWith, like this:</p>
<p><code>sumN 1 = primes<br />
sumN (n+1) = zipWith (+) primes $ tail $ sumN n</code></p>
<p>For extra prettiness, notice that primes == sumN 1 and that Andre&#8217;s version of intersect can be derived from Magnus&#8217; version of intersect using foldl1:</p>
<p><code>main = print $ head $ foldl1 intersect [sumN n primes | n &lt;- [1, 3, 29, 373, 665]]</code></p>
<p> <img src='http://therning.org/magnus/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Russell O'Connor</title>
		<link>http://therning.org/magnus/archives/353#comment-126110</link>
		<dc:creator>Russell O'Connor</dc:creator>
		<pubDate>Sun, 08 Jun 2008 14:50:02 +0000</pubDate>
		<guid isPermaLink="false">http://therning.org/magnus/?p=353#comment-126110</guid>
		<description>&lt;p&gt;This is almost the same as my code, except I wrote &lt;code&gt;sumN&lt;/code&gt; as &lt;code&gt;sumN xs = map (sum . take n) (tails primes)&lt;/code&gt;&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>This is almost the same as my code, except I wrote <code>sumN</code> as <code>sumN xs = map (sum . take n) (tails primes)</code></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Andre</title>
		<link>http://therning.org/magnus/archives/353#comment-126099</link>
		<dc:creator>Andre</dc:creator>
		<pubDate>Sun, 08 Jun 2008 11:33:19 +0000</pubDate>
		<guid isPermaLink="false">http://therning.org/magnus/?p=353#comment-126099</guid>
		<description>&lt;p&gt;Ah, I see. My problem was the naive implementation of the sieve. The optimized version runs much faster. :-)&lt;/p&gt;

&lt;p&gt;My intersection-function is similar, although it finds elements which are in each passed (infinite) list. Here's my implementation:
&lt;code&gt;
intersect :: (Eq a, Ord a) =&#62; [[a]] -&#62; [a]
intersect [] = []
intersect xs &#124; or (map null xs) = []
             &#124; and $ map ((== x) . head) xs = x : (intersect $ map tail xs)
             &#124; otherwise = intersect $ map (dropWhile (&#60; max)) xs
   where max = let comp (x:&lt;em&gt;) (y:&lt;/em&gt;) = compare x y
               in  head $ maximumBy comp xs
         x   = head $ head xs
&lt;/code&gt;
(Sorry for the formatting.)&lt;/p&gt;

&lt;p&gt;Therefore I've a more general function for finding the prime which fulfills the required conditions:
&lt;code&gt;
prime :: [Int] -&#62; Integer
prime = head . intersect . (primes:) . map (`calc` primes)
&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Whereas calc is semantically equivalent to your sumN-function.&lt;/p&gt;

&lt;p&gt;Using your test-data and the optimized sieve my program needs circa 10 seconds for finding the solution. Great work of yours!&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>Ah, I see. My problem was the naive implementation of the sieve. The optimized version runs much faster. <img src='http://therning.org/magnus/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
<p>My intersection-function is similar, although it finds elements which are in each passed (infinite) list. Here&#8217;s my implementation:<br />
<code><br />
intersect :: (Eq a, Ord a) =&gt; [[a]] -&gt; [a]<br />
intersect [] = []<br />
intersect xs | or (map null xs) = []<br />
             | and $ map ((== x) . head) xs = x : (intersect $ map tail xs)<br />
             | otherwise = intersect $ map (dropWhile (&lt; max)) xs<br />
   where max = let comp (x:<em>) (y:</em>) = compare x y<br />
               in  head $ maximumBy comp xs<br />
         x   = head $ head xs<br />
</code><br />
(Sorry for the formatting.)</p>
<p>Therefore I&#8217;ve a more general function for finding the prime which fulfills the required conditions:<br />
<code><br />
prime :: [Int] -&gt; Integer<br />
prime = head . intersect . (primes:) . map (`calc` primes)<br />
</code></p>
<p>Whereas calc is semantically equivalent to your sumN-function.</p>
<p>Using your test-data and the optimized sieve my program needs circa 10 seconds for finding the solution. Great work of yours!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: foobar</title>
		<link>http://therning.org/magnus/archives/353#comment-126081</link>
		<dc:creator>foobar</dc:creator>
		<pubDate>Sun, 08 Jun 2008 09:00:34 +0000</pubDate>
		<guid isPermaLink="false">http://therning.org/magnus/?p=353#comment-126081</guid>
		<description>&lt;p&gt;The following looks nicer to me:

sumN 1 = primes
sumN n = [x+y &#124; (x, y) &#60;- zip (tail $ sumN (n-1)) primes]
&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>The following looks nicer to me:</p>
<p>sumN 1 = primes<br />
sumN n = [x+y | (x, y) &lt;- zip (tail $ sumN (n-1)) primes]</p>
]]></content:encoded>
	</item>
</channel>
</rss>
