<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
<channel>
<title>Comments on snippet: 'C++ prime number generator'</title>
<link>http://snipplr.com</link>
<description>Snipplr comments feed'</description>
<language>en-us</language>
<pubDate>Mon, 20 May 2013 21:31:11 GMT</pubDate>
<item>
<title>advanceuser said on 7/2/11</title>
<link>http://snipplr.com/view/1219/c-prime-number-generator/</link>
<description><![CDATA[ *beautiful ]]></description>
<pubDate>Sat, 02 Jul 2011 02:05:11 GMT</pubDate>
<guid>http://snipplr.com/view/1219/c-prime-number-generator/</guid>
</item>
<item>
<title>advanceuser said on 7/2/11</title>
<link>http://snipplr.com/view/1219/c-prime-number-generator/</link>
<description><![CDATA[ Thank you,your code is beatiful and smart. ]]></description>
<pubDate>Sat, 02 Jul 2011 02:04:26 GMT</pubDate>
<guid>http://snipplr.com/view/1219/c-prime-number-generator/</guid>
</item>
<item>
<title>ejexion said on 2/22/11</title>
<link>http://snipplr.com/view/1219/c-prime-number-generator/</link>
<description><![CDATA[ Yes.

Starting with the first for-loop, we can see that, obviously, we are only testing for primes in [ 2 , 100000 [ .
Nothing too complicated there.

The next loop goes through the numbers that will be potential factors of our number 'i'. Starting with 2, this
value takes on larger and larger values, up to half of 'i' plus 1. 
The 'if' statement within that for-loop serves to stop checking if a number is prime as soon as it is evenly divided
by j. 

Finally, the last statement merely checks if j is bigger than half of i, and if it is, then i is a prime.


All of that explanation doesn't really explain why this works. I'll try to get that across here.

The for-loop with i in it can't be made any clearer; it is simply the number that we are checking
for primality.
What happens inside that outer for-loop is the interesting stuff. Basically, when a number is prime,
the inside for-loop will run until j is equal to half  of i (rounded down) plus 1. If a number is not prime,
it will "break" out of the if statement !( i % j ), since that statement is TRUE when i % j is 0.  So there
are two ways that a number can arrive at the last if-statement; either by being prime (and consequently
j being greater than i/2) or by not being prime and j being less i/2. Once that last if-statement is arrived
at, it's clear that only primes will be printed out. ]]></description>
<pubDate>Tue, 22 Feb 2011 03:40:37 GMT</pubDate>
<guid>http://snipplr.com/view/1219/c-prime-number-generator/</guid>
</item>
<item>
<title>Sweety said on 3/2/07</title>
<link>http://snipplr.com/view/1219/c-prime-number-generator/</link>
<description><![CDATA[ <p>could u explain how the function works?</p>
 ]]></description>
<pubDate>Fri, 02 Mar 2007 13:14:08 GMT</pubDate>
<guid>http://snipplr.com/view/1219/c-prime-number-generator/</guid>
</item>
</channel>
</rss>