<?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: Factoring Algorithm puzzle</title>
	<atom:link href="http://www.alternativerecursion.info/?feed=rss2&#038;p=823" rel="self" type="application/rss+xml" />
	<link>http://www.alternativerecursion.info/?p=823</link>
	<description>stuff from my brain</description>
	<lastBuildDate>Wed, 01 Sep 2010 17:47:28 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
	<item>
		<title>By: stephen</title>
		<link>http://www.alternativerecursion.info/?p=823#comment-142</link>
		<dc:creator>stephen</dc:creator>
		<pubDate>Thu, 18 Mar 2010 22:45:56 +0000</pubDate>
		<guid isPermaLink="false">http://www.alternativerecursion.info/?p=823#comment-142</guid>
		<description>so just for fun i ran a couple tests with the sieve of eratosthenes vs the brute mod division algorithm i used,

&lt;pre class=&quot;brush: java;&quot;&gt;
public class primes {

	public static ArrayList&lt;Integer&gt; prime_list = new ArrayList&lt;Integer&gt;();
	public static void main(String args[]){
		int maxx = 166;

		long t0 = System.nanoTime();
		boolean[] excludearray = new boolean[maxx+1];
		for(int n=2;n&lt;=maxx;n++){
			if(excludearray[n] == false){
				prime_list.add(n);
				for(int m=2; m&lt;=maxx/n;m++){
					excludearray[n*m] = true;

				}
			}
		}
		//System.out.println(&quot;the primes less than 167 are:&quot;);
		//System.out.println(prime_list);
		//System.out.println(&quot;there are &quot;   prime_list.size()   &quot; of them&quot;);
		long time1 = System.nanoTime()-t0;


		prime_list = new ArrayList&lt;Integer&gt;();
		long t2 = System.nanoTime();
		boolean isPrime;
			for(int n=2; n &lt;= maxx; n++){
			isPrime = true;
			for(int p : prime_list){
				if(n % p == 0){
					isPrime = false;
					break;
				}
			}
			if(isPrime){
				prime_list.add(n);
			}
		}
		//System.out.println(&quot;the primes less than 167 are:&quot;);
		//System.out.println(prime_list);
		//System.out.println(&quot;there are &quot;   prime_list.size()   &quot; of them&quot;);
		long time2 = System.nanoTime()-t2;
		System.out.println(time1 +  &quot; nanoseconds for sieve for primes under &quot; + (maxx+1));
		System.out.println(time2 + &quot; nanoseconds for brute for primes under &quot;+ (maxx+1));
&lt;/pre&gt;
the output is:
&lt;pre&gt;
PS C:\workspace\p\src&gt; javac primes.java
PS C:\workspace\p\src&gt; java primes
522853 nanoseconds for sieve for primes under 167
471033 nanoseconds for brute for primes under 167
&lt;/pre&gt;

so the brute force is slightly faster for the first 38 primes, but lets look at a larger set of primes, I imagine that boolean array will do far more than make up for its creation-time-cost:

for the first 15,173 primes:
&lt;pre&gt;
PS C:\workspace\p\src&gt; javac primes.java
PS C:\workspace\p\src&gt; java primes
23320782 nanoseconds for sieve for primes under 166001
992025022 nanoseconds for brute for primes under 166001
&lt;/pre&gt;</description>
		<content:encoded><![CDATA[<p>so just for fun i ran a couple tests with the sieve of eratosthenes vs the brute mod division algorithm i used,</p>
<pre class="brush: java;">
public class primes {

	public static ArrayList&lt;Integer&gt; prime_list = new ArrayList&lt;Integer&gt;();
	public static void main(String args[]){
		int maxx = 166;

		long t0 = System.nanoTime();
		boolean[] excludearray = new boolean[maxx+1];
		for(int n=2;n&lt;=maxx;n++){
			if(excludearray[n] == false){
				prime_list.add(n);
				for(int m=2; m&lt;=maxx/n;m++){
					excludearray[n*m] = true;

				}
			}
		}
		//System.out.println(&quot;the primes less than 167 are:&quot;);
		//System.out.println(prime_list);
		//System.out.println(&quot;there are &quot;   prime_list.size()   &quot; of them&quot;);
		long time1 = System.nanoTime()-t0;

		prime_list = new ArrayList&lt;Integer&gt;();
		long t2 = System.nanoTime();
		boolean isPrime;
			for(int n=2; n &lt;= maxx; n++){
			isPrime = true;
			for(int p : prime_list){
				if(n % p == 0){
					isPrime = false;
					break;
				}
			}
			if(isPrime){
				prime_list.add(n);
			}
		}
		//System.out.println(&quot;the primes less than 167 are:&quot;);
		//System.out.println(prime_list);
		//System.out.println(&quot;there are &quot;   prime_list.size()   &quot; of them&quot;);
		long time2 = System.nanoTime()-t2;
		System.out.println(time1 +  &quot; nanoseconds for sieve for primes under &quot; + (maxx+1));
		System.out.println(time2 + &quot; nanoseconds for brute for primes under &quot;+ (maxx+1));
</pre>
<p>the output is:</p>
<pre>
PS C:\workspace\p\src> javac primes.java
PS C:\workspace\p\src> java primes
522853 nanoseconds for sieve for primes under 167
471033 nanoseconds for brute for primes under 167
</pre>
<p>so the brute force is slightly faster for the first 38 primes, but lets look at a larger set of primes, I imagine that boolean array will do far more than make up for its creation-time-cost:</p>
<p>for the first 15,173 primes:</p>
<pre>
PS C:\workspace\p\src> javac primes.java
PS C:\workspace\p\src> java primes
23320782 nanoseconds for sieve for primes under 166001
992025022 nanoseconds for brute for primes under 166001
</pre>
]]></content:encoded>
	</item>
	<item>
		<title>By: stephen</title>
		<link>http://www.alternativerecursion.info/?p=823#comment-141</link>
		<dc:creator>stephen</dc:creator>
		<pubDate>Thu, 18 Mar 2010 18:41:49 +0000</pubDate>
		<guid isPermaLink="false">http://www.alternativerecursion.info/?p=823#comment-141</guid>
		<description>I saw in python n%d is mod division, and yields the remainder, this could/should be much faster than comparing the float of the fraction with the floor of the float of the fraction, which is a round-about way to check if d divides n evenly.

When finding the first 15,173 primes, the &#039;float&#039; to the &#039;floor of float&#039; comparison took 129 seconds, mod-division took 34 seconds.

specifically :

&lt;pre class=&quot;brush: python;&quot;&gt;
import time
t0=time.time()
prime_list = []
maxx = 166000
for n in range(2,maxx+1):
    isprime = True
    for p in prime_list:
        if n/p == int (n/p):
            isprime = False;
            break;
    if isprime == True:
        prime_list.append(n)
print(&quot;the primes less than&quot;,maxx+1,&quot;are:&quot;)
#print(prime_list)
print(&quot;there are &quot;, len(prime_list), &quot;of them&quot;)
print((time.time()-t0),&quot;secs&quot;)
&lt;/pre&gt;

vs

&lt;pre class=&quot;brush: python;&quot;&gt;
import time
t0=time.time()
prime_list = []
maxx = 166000
for n in range(2,maxx+1):
    isprime = True
    for p in prime_list:
        if n%p is 0;
            isprime = False;
            break;
    if isprime == True:
        prime_list.append(n)
print(&quot;the primes less than&quot;,maxx+1,&quot;are:&quot;)
#print(prime_list)
print(&quot;there are &quot;, len(prime_list), &quot;of them&quot;)
print((time.time()-t0),&quot;secs&quot;)
&lt;/pre&gt;

oh yeah, in java the the times were 1.6 seconds vs 0.98 seconds respectively.(!)</description>
		<content:encoded><![CDATA[<p>I saw in python n%d is mod division, and yields the remainder, this could/should be much faster than comparing the float of the fraction with the floor of the float of the fraction, which is a round-about way to check if d divides n evenly.</p>
<p>When finding the first 15,173 primes, the &#8216;float&#8217; to the &#8216;floor of float&#8217; comparison took 129 seconds, mod-division took 34 seconds.</p>
<p>specifically :</p>
<pre class="brush: python;">
import time
t0=time.time()
prime_list = []
maxx = 166000
for n in range(2,maxx+1):
    isprime = True
    for p in prime_list:
        if n/p == int (n/p):
            isprime = False;
            break;
    if isprime == True:
        prime_list.append(n)
print("the primes less than",maxx+1,"are:")
#print(prime_list)
print("there are ", len(prime_list), "of them")
print((time.time()-t0),"secs")
</pre>
<p>vs</p>
<pre class="brush: python;">
import time
t0=time.time()
prime_list = []
maxx = 166000
for n in range(2,maxx+1):
    isprime = True
    for p in prime_list:
        if n%p is 0;
            isprime = False;
            break;
    if isprime == True:
        prime_list.append(n)
print("the primes less than",maxx+1,"are:")
#print(prime_list)
print("there are ", len(prime_list), "of them")
print((time.time()-t0),"secs")
</pre>
<p>oh yeah, in java the the times were 1.6 seconds vs 0.98 seconds respectively.(!)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: stephen</title>
		<link>http://www.alternativerecursion.info/?p=823#comment-140</link>
		<dc:creator>stephen</dc:creator>
		<pubDate>Thu, 18 Mar 2010 07:29:37 +0000</pubDate>
		<guid isPermaLink="false">http://www.alternativerecursion.info/?p=823#comment-140</guid>
		<description>The sieve of eratosthenes (I never knew the name until now) &lt;b&gt;is&lt;/b&gt; asymptotically more efficient in cpu use, although it&#039;s a bit more complex to program and requires memory use.

It&#039;s nearly instant either way, but do you think in this case (first 38 primes) it makes up for the extra cost of creating/allocating the boolean array?

The density of primes decreases as you get further down the number line ( it&#039;s like ~1/ln(n) ), so I could see this algorithm becoming a must-use for finding a larger number of primes. (although lower density of primes means higher memory use per prime for the sieve of eratosthenes as we look for larger and larger primes)


Thanks.
btw didn&#039;t know you could iterate through an ArrayList with that syntax like you can in python with the for loop.  nice.</description>
		<content:encoded><![CDATA[<p>The sieve of eratosthenes (I never knew the name until now) <b>is</b> asymptotically more efficient in cpu use, although it&#8217;s a bit more complex to program and requires memory use.</p>
<p>It&#8217;s nearly instant either way, but do you think in this case (first 38 primes) it makes up for the extra cost of creating/allocating the boolean array?</p>
<p>The density of primes decreases as you get further down the number line ( it&#8217;s like ~1/ln(n) ), so I could see this algorithm becoming a must-use for finding a larger number of primes. (although lower density of primes means higher memory use per prime for the sieve of eratosthenes as we look for larger and larger primes)</p>
<p>Thanks.<br />
btw didn&#8217;t know you could iterate through an ArrayList with that syntax like you can in python with the for loop.  nice.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Whitney Sorenson</title>
		<link>http://www.alternativerecursion.info/?p=823#comment-139</link>
		<dc:creator>Whitney Sorenson</dc:creator>
		<pubDate>Wed, 17 Mar 2010 21:20:43 +0000</pubDate>
		<guid isPermaLink="false">http://www.alternativerecursion.info/?p=823#comment-139</guid>
		<description>First of all, you can clean up your java:

&lt;pre class=&quot;brush: java;&quot;&gt;
import java.util.ArrayList;

public class primes {
	public static void main(String args[]) {
		ArrayList prime_list = new ArrayList();
		for (int n = 2; n &lt;= 166; n++) {
			boolean isprime = true;
			for (int p : prime_list) {
				if ((n / p) == (float) n / p) {
					isprime = false;
					break;
				}
			}
			if (isprime) {
				prime_list.add(n);
			}
		}
		System.out.println(&quot;the primes less than 167 are:&quot;);
		System.out.println(prime_list);
		System.out.println(&quot;there are &quot;  + prime_list.size() +  &quot; of them&quot;);
	}
&lt;/pre&gt;

But really, you should use a sieve of eratosthenes:

&lt;pre class=&quot;brush: java;&quot;&gt;
import java.util.ArrayList;

public class primes {
	public static void main(String args[]) {
		ArrayList prime_list = new ArrayList();

		int upperBoundSquareRoot = (int) Math.sqrt(166);
		boolean[] isComposite = new boolean[166 + 1];
		for (int m = 2; m &lt;= upperBoundSquareRoot; m  ) {
			if (!isComposite[m]) {
				prime_list.add(m);
				for (int k = m * m; k &lt;= 166; k  = m)
					isComposite[k] = true;
			}
		}
		for (int m = upperBoundSquareRoot; m &lt;= 166; m++)
			if (!isComposite[m])
				prime_list.add(m);

		System.out.println(&quot;the primes less than 167 are:&quot;);
		System.out.println(prime_list);
		System.out.println(&quot;there are &quot; +  prime_list.size() +  &quot; of them&quot;);
	}

}
&lt;/pre&gt;</description>
		<content:encoded><![CDATA[<p>First of all, you can clean up your java:</p>
<pre class="brush: java;">
import java.util.ArrayList;

public class primes {
	public static void main(String args[]) {
		ArrayList prime_list = new ArrayList();
		for (int n = 2; n &lt;= 166; n++) {
			boolean isprime = true;
			for (int p : prime_list) {
				if ((n / p) == (float) n / p) {
					isprime = false;
					break;
				}
			}
			if (isprime) {
				prime_list.add(n);
			}
		}
		System.out.println(&quot;the primes less than 167 are:&quot;);
		System.out.println(prime_list);
		System.out.println(&quot;there are &quot;  + prime_list.size() +  &quot; of them&quot;);
	}
</pre>
<p>But really, you should use a sieve of eratosthenes:</p>
<pre class="brush: java;">
import java.util.ArrayList;

public class primes {
	public static void main(String args[]) {
		ArrayList prime_list = new ArrayList();

		int upperBoundSquareRoot = (int) Math.sqrt(166);
		boolean[] isComposite = new boolean[166 + 1];
		for (int m = 2; m &lt;= upperBoundSquareRoot; m  ) {
			if (!isComposite[m]) {
				prime_list.add(m);
				for (int k = m * m; k &lt;= 166; k  = m)
					isComposite[k] = true;
			}
		}
		for (int m = upperBoundSquareRoot; m &lt;= 166; m++)
			if (!isComposite[m])
				prime_list.add(m);

		System.out.println(&quot;the primes less than 167 are:&quot;);
		System.out.println(prime_list);
		System.out.println(&quot;there are &quot; +  prime_list.size() +  &quot; of them&quot;);
	}

}
</pre>
]]></content:encoded>
	</item>
</channel>
</rss>
