The original implementation is was has been renamed by ul to isProbablyPrime<div>Note that the probability is according to Knuth&#39;s words</div><blockquote class="webkit-indent-blockquote" style="margin: 0 0 0 40px; border: none; padding: 0px;">
<div>...less than (1/4)^25 that such a 25-time-in-a-row procedure gives the wrong</div><div>information about its input. This is less than one chance in a quadrillion; [...]</div><div>It&#39;s much more likely that our computer has dropped a bit in its calculations,</div>
<div>due to hardware malfunctions or cosmic radiation, than that Algorithm P has</div><div>repeatedly guessed wrong!</div></blockquote>So &#39;probabilistic&#39; in this case is very much deterministic!<div>For accessible rationale about the algoritm (and a non probabilistic better one as well)<br>
<div>you can also see &quot;<a href="http://mitpress.mit.edu/sicp/full-text/book/book-Z-H-11.html#%_sec_1.2.6">1.2.6 Example: Testing for Primality</a>&quot; in SICP.</div><div>A possible improvement could have been to keep a list of the first N</div>
<div>prime numbers (i.e. N=1000 or whatever integrer where gain in performance</div><div>and or memory) and resort to the probabilistic test if self</div><div>is greater than the bigger in the list.</div><div><br></div><div>
The value of original algorithm is all research and reasoning that went into it from</div><div>Knuth et al. (note the order of growth is log(n), where n is the integer under scrutiny)</div><div>The problem with the new implementation is that it goes thru testing numbers which are</div>
<div>clearly not prime, 5, 15, 25, 35...just to mention multiples of 5.</div><div>It can possibly be faster for small integers hence the above possible</div><div>improvement suggestion...but for the rest it should be thrown away IMHO.</div>
<div><br></div><div>Primality testing is very important for modern electronic communications, i.e. encryption</div><div>and as such it has to be reliable and performant for big integers.</div><div><div><br></div><div>Hope this clarifies</div>
<div>Bye</div><div>Enrico<br>PS: the comment in the code was explicit enough to allow to research for</div><div>      the rationale about the algorithm...we should not fix what works (well)</div><div>      there are so many other fixes waiting...<br>
<div class="gmail_quote">On Sat, Dec 19, 2009 at 12:56 AM, David T. Lewis <span dir="ltr">&lt;<a href="mailto:lewis@mail.msen.com">lewis@mail.msen.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
<div class="im">On Fri, Dec 18, 2009 at 05:25:45PM +0100, Enrico Spinielli wrote:<br>
&gt; Hi all,<br>
&gt; I am back to checking Squeak after quite a while and got latest trunk.<br>
&gt; I looked after one of my contributions Integer&gt;&gt;isPrime<br>
&gt; and I found my implementation of Algorithm P from Knuth&#39;s AOCP vol 2<br>
&gt; substituted by an iteration of dividing self by all even numbers starting<br>
&gt; from 3<br>
&gt; and (correctly) stopping at self sqrtFloor.<br>
&gt; This is IMHO a questionable/useless &quot;improvement&quot;, not even looking to try<br>
&gt; to implement the Sieve of Eratostene...!<br>
&gt;<br>
&gt; Again IMHO isPrime should be reverted back to what has been renamed<br>
&gt; isProbablyPrime<br>
&gt;<br>
&gt; Not being anymore used to contribute I just signal it here...<br>
&gt;<br>
&gt; Hope it helps<br>
&gt; Bye<br>
&gt; --<br>
&gt; Enrico Spinielli<br>
<br>
</div>Enrico,<br>
<br>
Is this your original implementation?<br>
<br>
<br>
   isPrime<br>
        &quot;See isProbablyPrimeWithK:andQ: for the algoritm description.&quot;<br>
        | k q |<br>
        self &lt;= 1 ifTrue: [^self error: &#39;operation undefined&#39;].<br>
        self even ifTrue: [^self = 2].<br>
        k := 1.<br>
<br>
        q := self - 1 bitShift: -1.<br>
        [q odd] whileFalse:<br>
                        [q := q bitShift: -1.<br>
                        k := k + 1].<br>
<br>
        25 timesRepeat: [(self isProbablyPrimeWithK: k andQ: q) ifFalse: [^false]].<br>
        ^true<br>
<br>
It was recently changed as follows:<br>
<br>
&gt; Name: Kernel-ul.305<br>
&gt; Author: ul<br>
&gt; Time: 25 November 2009, 2:55:43.339 am<br>
&gt; UUID: a95be01c-d87c-154b-bdc6-c582dafad80b<br>
&gt; Ancestors: Kernel-nice.304<br>
&gt;<br>
&gt; - added Integer &gt;&gt; #sqrtFloor, which returns the floor of the square root of the receiver.<br>
&gt; - renamed Integer &gt;&gt; #isPrime to #isProbablyPrime.<br>
&gt; - added Integer &gt;&gt; #isPrime which is implemented as a deterministic primality test<br>
&gt; - both #isPrime and #isProbablyPrime return false for receivers &lt;= 1 instead of raising an error<br>
<br>
Is this a reasonable change?<br>
<br>
Dave<br>
<br>
<br>
</blockquote></div><br><br clear="all"><br>-- <br>Enrico Spinielli<br>&quot;Do Androids dream of electric sheep?&quot;— Philip K. Dick<br>&quot;Hear and forget; see and remember;do and understand.&quot;—Mitchel Resnick<br>

</div></div></div>