<?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"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Groovy Prime Numbers</title>
	<atom:link href="http://www.thejavajar.com/2009/05/11/groovy-prime-numbers/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.thejavajar.com/2009/05/11/groovy-prime-numbers/</link>
	<description>{ java, groovy, flex, python, ruby }</description>
	<lastBuildDate>Mon, 26 Jul 2010 17:15:16 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
	<item>
		<title>By: Peter Lewerin</title>
		<link>http://www.thejavajar.com/2009/05/11/groovy-prime-numbers/comment-page-1/#comment-31</link>
		<dc:creator>Peter Lewerin</dc:creator>
		<pubDate>Thu, 04 Jun 2009 14:51:38 +0000</pubDate>
		<guid isPermaLink="false">http://www.thejavajar.com/?p=113#comment-31</guid>
		<description>@Nurul - very good!</description>
		<content:encoded><![CDATA[<p>@Nurul &#8211; very good!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: KrisBelucci</title>
		<link>http://www.thejavajar.com/2009/05/11/groovy-prime-numbers/comment-page-1/#comment-30</link>
		<dc:creator>KrisBelucci</dc:creator>
		<pubDate>Tue, 02 Jun 2009 03:12:46 +0000</pubDate>
		<guid isPermaLink="false">http://www.thejavajar.com/?p=113#comment-30</guid>
		<description>da best. Keep it going! Thank you</description>
		<content:encoded><![CDATA[<p>da best. Keep it going! Thank you</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: FredJouldd</title>
		<link>http://www.thejavajar.com/2009/05/11/groovy-prime-numbers/comment-page-1/#comment-29</link>
		<dc:creator>FredJouldd</dc:creator>
		<pubDate>Thu, 28 May 2009 21:11:22 +0000</pubDate>
		<guid isPermaLink="false">http://www.thejavajar.com/?p=113#comment-29</guid>
		<description>Thanks, good article.</description>
		<content:encoded><![CDATA[<p>Thanks, good article.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: UnennyMive</title>
		<link>http://www.thejavajar.com/2009/05/11/groovy-prime-numbers/comment-page-1/#comment-28</link>
		<dc:creator>UnennyMive</dc:creator>
		<pubDate>Sun, 24 May 2009 15:12:15 +0000</pubDate>
		<guid isPermaLink="false">http://www.thejavajar.com/?p=113#comment-28</guid>
		<description>Hi, courteous posts there :-) express&#039;s recompense the gripping information</description>
		<content:encoded><![CDATA[<p>Hi, courteous posts there <img src='http://www.thejavajar.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' />  express&#8217;s recompense the gripping information</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Nurul Choudhury</title>
		<link>http://www.thejavajar.com/2009/05/11/groovy-prime-numbers/comment-page-1/#comment-27</link>
		<dc:creator>Nurul Choudhury</dc:creator>
		<pubDate>Sat, 16 May 2009 03:53:46 +0000</pubDate>
		<guid isPermaLink="false">http://www.thejavajar.com/?p=113#comment-27</guid>
		<description>Sorry the code again and it is very fast, for 100,000 it takes less than a second:


def t = (0..10000).flatten()

t[0]=0; t[1]=0; // 0 and 1 are not prime

def L = Math.sqrt(t.size()-1)

( [2,(3..L).step(2)].flatten()).each { n -&gt;
     if(t[n]) {
        def delta = n==2?1:2;
       (((n*n)..(t.size())).step(n*delta)).each {
          i -&gt; t[i] = 0
       }
 }
}

println t.findAll({ it != 0 })</description>
		<content:encoded><![CDATA[<p>Sorry the code again and it is very fast, for 100,000 it takes less than a second:</p>
<p>def t = (0..10000).flatten()</p>
<p>t[0]=0; t[1]=0; // 0 and 1 are not prime</p>
<p>def L = Math.sqrt(t.size()-1)</p>
<p>( [2,(3..L).step(2)].flatten()).each { n -&gt;<br />
     if(t[n]) {<br />
        def delta = n==2?1:2;<br />
       (((n*n)..(t.size())).step(n*delta)).each {<br />
          i -&gt; t[i] = 0<br />
       }<br />
 }<br />
}</p>
<p>println t.findAll({ it != 0 })</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Nurul Choudhury</title>
		<link>http://www.thejavajar.com/2009/05/11/groovy-prime-numbers/comment-page-1/#comment-25</link>
		<dc:creator>Nurul Choudhury</dc:creator>
		<pubDate>Sat, 16 May 2009 03:42:27 +0000</pubDate>
		<guid isPermaLink="false">http://www.thejavajar.com/?p=113#comment-25</guid>
		<description>Some nice solutions and I particularly like Lewerin&#039;s solution but it is still rather slow for say 100,000.

There are same simple things to improve the performance
1. Removing evements from an ArrayList [...] is very expensive. So keep the list size the same and set the the value at an index to zero if the number is not prime.

So if   x is not prime then t[x] = 0

2. The outer list outer list has to go over 2 and odd numbers only.

    [2, (3..L).step(2)].flatten()   will do the trick
    (L is sqrt(max_num))
    so we get the list [2,3,5,7,9...]

So where does the sqrt come from?

Well...    if X is a composite number (non prime) then
        X = A*B

let say we always arrange it so that A is always the smallest value such that X = A*B, then the largest value for A can be sqrt(X).  (beccause X = sqrt(X)*sqrt(X) )

for the inner loop we don&#039;t have to start fro 2*n if n &gt; 2, because we have already removed all multiples of 2. If n &gt; 3 we don&#039;t have to start from 3*n because we have removed all multiples of 3 and so on.

By that logic we can start from n*n; also n would itself have to be prime, otherwise it already has been removed. So we remove - n*n, n*(n+1), n*(n+2) ...

But wait a minute if n is prime and n &gt; 2 the n is also odd; and n+1 must be even (a multiple of 2), but we have removed all those. So we actually need to remove
n*n, n*(n+2), n*(n+4), ...

so for n &gt; 2 the step size in the inner loop is 2*n
def t = (0..10000).flatten()
t[0]=0; t[1]=0; // 0 and 1 are not prime
 def L = Math.sqrt(t.size())
 ( [2,(3..L).step(2)].flatten()).each { n -&gt;
     if(t[n])  {
          def delta = n==2?1:2;
         (((n*n)..(t.size())).step(n*delta)).each({i -&gt;  t[i] = 0 })
     }
  }

result = t.findAll({ it != 0 })</description>
		<content:encoded><![CDATA[<p>Some nice solutions and I particularly like Lewerin&#8217;s solution but it is still rather slow for say 100,000.</p>
<p>There are same simple things to improve the performance<br />
1. Removing evements from an ArrayList [...] is very expensive. So keep the list size the same and set the the value at an index to zero if the number is not prime.</p>
<p>So if   x is not prime then t[x] = 0</p>
<p>2. The outer list outer list has to go over 2 and odd numbers only.</p>
<p>    [2, (3..L).step(2)].flatten()   will do the trick<br />
    (L is sqrt(max_num))<br />
    so we get the list [2,3,5,7,9...]</p>
<p>So where does the sqrt come from?</p>
<p>Well&#8230;    if X is a composite number (non prime) then<br />
        X = A*B</p>
<p>let say we always arrange it so that A is always the smallest value such that X = A*B, then the largest value for A can be sqrt(X).  (beccause X = sqrt(X)*sqrt(X) )</p>
<p>for the inner loop we don&#8217;t have to start fro 2*n if n &gt; 2, because we have already removed all multiples of 2. If n &gt; 3 we don&#8217;t have to start from 3*n because we have removed all multiples of 3 and so on.</p>
<p>By that logic we can start from n*n; also n would itself have to be prime, otherwise it already has been removed. So we remove &#8211; n*n, n*(n+1), n*(n+2) &#8230;</p>
<p>But wait a minute if n is prime and n &gt; 2 the n is also odd; and n+1 must be even (a multiple of 2), but we have removed all those. So we actually need to remove<br />
n*n, n*(n+2), n*(n+4), &#8230;</p>
<p>so for n &gt; 2 the step size in the inner loop is 2*n<br />
def t = (0..10000).flatten()<br />
t[0]=0; t[1]=0; // 0 and 1 are not prime<br />
 def L = Math.sqrt(t.size())<br />
 ( [2,(3..L).step(2)].flatten()).each { n -&gt;<br />
     if(t[n])  {<br />
          def delta = n==2?1:2;<br />
         (((n*n)..(t.size())).step(n*delta)).each({i -&gt;  t[i] = 0 })<br />
     }<br />
  }</p>
<p>result = t.findAll({ it != 0 })</p>
]]></content:encoded>
	</item>
</channel>
</rss>
