<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Primality on Rohit Jha</title><link>https://www.rohitjha.dev/tags/primality/</link><description>Recent content in Primality on Rohit Jha</description><generator>Hugo</generator><language>en-US</language><copyright>© Rohit Jha. Articles licensed under CC BY-SA 4.0 unless noted.</copyright><lastBuildDate>Thu, 23 Jul 2026 16:53:18 +0530</lastBuildDate><atom:link href="https://www.rohitjha.dev/tags/primality/index.xml" rel="self" type="application/rss+xml"/><item><title>0x000b - Sieve of Eratosthenes</title><link>https://www.rohitjha.dev/blog/sieve-of-eratosthenes/</link><pubDate>Tue, 01 Nov 2011 20:19:54 +0000</pubDate><guid>https://www.rohitjha.dev/blog/sieve-of-eratosthenes/</guid><description>&lt;p&gt;The Sieve of Eratosthenes, named after the Greek mathematician Eratosthenes, is one of many known prime number sieves. The algorithm is a fairly good one for finding prime numbers from a list of numbers, say, less than 10 million.&lt;/p&gt;
&lt;p&gt;Suppose we need to find all prime numbers below a natural number &lt;em&gt;n &amp;gt;&lt;/em&gt; 1. Then, the algorithm is given as:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Create a list of consecutive integers from 2 to &lt;em&gt;n&lt;/em&gt;: (2, 3, 4, …, &lt;em&gt;n&lt;/em&gt;).&lt;/li&gt;
&lt;li&gt;Initially, let &lt;em&gt;p&lt;/em&gt; equal 2, the first prime number.&lt;/li&gt;
&lt;li&gt;Starting from &lt;em&gt;p&lt;/em&gt;, count up in increments of &lt;em&gt;p&lt;/em&gt;, as long as the value is less than √&lt;em&gt;n&lt;/em&gt; and mark each of these numbers greater than &lt;em&gt;p&lt;/em&gt; itself in the list. These numbers will be 2&lt;em&gt;p&lt;/em&gt;, 3&lt;em&gt;p&lt;/em&gt;, 4&lt;em&gt;p&lt;/em&gt;, etc.; note that some of them may have already been marked.&lt;/li&gt;
&lt;li&gt;Find the first number greater than &lt;em&gt;p&lt;/em&gt; in the list that is not marked; let &lt;em&gt;p&lt;/em&gt; now equal this number (which is the next prime).&lt;/li&gt;
&lt;li&gt;If there were no more unmarked numbers in the list, stop. Otherwise, repeat from step 3.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;When, the above algorithm terminates, all unmarked numbers are prime.&lt;/p&gt;</description></item><item><title>0x0007 - Primality Testing</title><link>https://www.rohitjha.dev/blog/primality-testing/</link><pubDate>Sat, 29 Oct 2011 22:58:18 +0000</pubDate><guid>https://www.rohitjha.dev/blog/primality-testing/</guid><description>&lt;p&gt;I’ve been doing some programs on &lt;a href="http://en.wikipedia.org/wiki/Primality_test"&gt;Primality tests&lt;/a&gt;, and so far, have implemented some naive algorithms. Though not efficient, these are useful for quick implementations. Here I post some of them:&lt;/p&gt;
&lt;p&gt;&lt;em&gt;&lt;strong&gt;Method 1:&lt;/strong&gt;&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;The simplest primality test is as follows: Given an input number &lt;em&gt;n&lt;/em&gt;, check whether any integer &lt;em&gt;m&lt;/em&gt; from 2 to &lt;em&gt;n&lt;/em&gt; − 1 evenly divides &lt;em&gt;n&lt;/em&gt;. If &lt;em&gt;n&lt;/em&gt; is divisible by any &lt;em&gt;m&lt;/em&gt; then &lt;em&gt;n&lt;/em&gt; is composite, otherwise it is prime. The implementation in Python 2.7 in the form of a function is:&lt;/p&gt;</description></item></channel></rss>