No need to thank me, I just enjoyed to implement an algorithm in Squeak and signed for MIT license:<div>so use/abuse/misuse/... freely.<div>We all have to thank you and the others for looking after contributions and make them available</div>
<div>in Trunk or images.</div><div><br></div><div>I think that what Levente discovered from DigitalSignatureAlgorithm&gt;&gt;isProbablyPrime:</div><div>is worth some thoughts in terms of cleanup and rationalisation...from the version info this</div>
<div>piece of code seems to precede mine...and confirm the need of primality testing for encryption</div><div>(hence big integers)</div><div><br></div><div>Bye</div><div>Enrico</div><div>PS: I tried to see how to contribute, i.e. in the inbox but did not find too much of instructions</div>
<div>(something in Squeak board blog but not too clear for me at least). Any suggestion? URL?<br><br><div class="gmail_quote">On Sat, Jan 23, 2010 at 9:51 PM, 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;">To follow up on this discussion from last month, I updated Squeak trunk<br>
such that LargePositiveInteger uses the probabilistic algorithm for<br>
#isPrime, and added method comments to explain. A couple of folks suggested<br>
this change, and Enrico concurred.<br>
<br>
It turns out that SmallInteger maxVal is a reasonable point at which<br>
to switch from use of #isPrime to #isProbablyPrime. On my system before<br>
the change:<br>
<br>
  count := 1000.<br>
  largeMin := SmallInteger maxVal + 1.<br>
<br>
  &quot;SmallInteger values up to maxVal&quot;<br>
  Time millisecondsToRun: [(SmallInteger maxVal - count to: SmallInteger maxVal) do: [:e | e isPrime]]. ==&gt; 120<br>
  Time millisecondsToRun: [(SmallInteger maxVal - count to: SmallInteger maxVal) do: [:e | e isProbablyPrime]]. ==&gt; 984<br>
<br>
  &quot;LargePositiveInteger values just above SmallInteger maxVal&quot;<br>
  Time millisecondsToRun: [(largeMin to: largeMin + count) do: [:e | e isPrime]]. ==&gt; 6599<br>
  Time millisecondsToRun: [(largeMin to: largeMin + count) do: [:e | e isProbablyPrime]]. ==&gt; 714<br>
<br>
After changing LargePositiveInteger&gt;&gt;isPrime, we have:<br>
<br>
  &quot;LargePositiveInteger values just above SmallInteger maxVal&quot;<br>
  Time millisecondsToRun: [(largeMin to: largeMin + count) do: [:e | e isPrime]]. ==&gt; 719<br>
<br>
So the implementation of LargePositiveInteger&gt;&gt;isPrime is now this:<br>
<br>
isPrime<br>
        &quot;Answer true if the receiver is a prime number. Use a probabilistic implementation       that<br>
        is much faster for large integers, and that is correct to an extremely high statistical<br>
        level of confidence (effectively deterministic).&quot;<br>
<br>
        ^ self isProbablyPrime<br>
<br>
Thanks to Enrico for his patience ;-)<br>
<br>
Dave<br>
<br>
<br>
On Sat Dec 19 13:58:16 UTC 2009, Enrico Spinielli wrote:<br>
&gt;<br>
&gt; The original implementation is was has been renamed by ul to isProbablyPrime<br>
&gt; Note that the probability is according to Knuth&#39;s words<br>
&gt;<br>
&gt; ...less than (1/4)^25 that such a 25-time-in-a-row procedure gives the wrong<br>
&gt; information about its input. This is less than one chance in a quadrillion;<br>
&gt; [...]<br>
&gt; It&#39;s much more likely that our computer has dropped a bit in its<br>
&gt; calculations,<br>
&gt; due to hardware malfunctions or cosmic radiation, than that Algorithm P has<br>
&gt; repeatedly guessed wrong!<br>
&gt;<br>
&gt; So &#39;probabilistic&#39; in this case is very much deterministic!<br>
&gt; For accessible rationale about the algoritm (and a non probabilistic better<br>
&gt; one as well)<br>
&gt; you can also see &quot;1.2.6 Example: Testing for<br>
&gt; Primality&lt;<a href="http://mitpress.mit.edu/sicp/full-text/book/book-Z-H-11.html#%_sec_1.2.6" target="_blank">http://mitpress.mit.edu/sicp/full-text/book/book-Z-H-11.html#%_sec_1.2.6</a>&gt;&quot;<br>
&gt; in SICP.<br>
&gt; A possible improvement could have been to keep a list of the first N<br>
&gt; prime numbers (i.e. N=1000 or whatever integrer where gain in performance<br>
&gt; and or memory) and resort to the probabilistic test if self<br>
&gt; is greater than the bigger in the list.<br>
&gt;<br>
&gt; The value of original algorithm is all research and reasoning that went into<br>
&gt; it from<br>
&gt; Knuth et al. (note the order of growth is log(n), where n is the integer<br>
&gt; under scrutiny)<br>
&gt; The problem with the new implementation is that it goes thru testing numbers<br>
&gt; which are<br>
&gt; clearly not prime, 5, 15, 25, 35...just to mention multiples of 5.<br>
&gt; It can possibly be faster for small integers hence the above possible<br>
&gt; improvement suggestion...but for the rest it should be thrown away IMHO.<br>
&gt;<br>
&gt; Primality testing is very important for modern electronic communications,<br>
&gt; i.e. encryption<br>
&gt; and as such it has to be reliable and performant for big integers.<br>
&gt;<br>
&gt; Hope this clarifies<br>
&gt; Bye<br>
&gt; Enrico<br>
&gt; PS: the comment in the code was explicit enough to allow to research for<br>
&gt;       the rationale about the algorithm...we should not fix what works<br>
&gt; (well)<br>
&gt;       there are so many other fixes waiting...<br>
&gt; On Sat, Dec 19, 2009 at 12:56 AM, David T. Lewis &lt;lewis at <a href="http://mail.msen.com" target="_blank">mail.msen.com</a>&gt;wrote:<br>
&gt;<br>
&gt; &gt; On Fri, Dec 18, 2009 at 05:25:45PM +0100, Enrico Spinielli wrote:<br>
&gt; &gt; &gt; Hi all,<br>
&gt; &gt; &gt; I am back to checking Squeak after quite a while and got latest trunk.<br>
&gt; &gt; &gt; I looked after one of my contributions Integer&gt;&gt;isPrime<br>
&gt; &gt; &gt; and I found my implementation of Algorithm P from Knuth&#39;s AOCP vol 2<br>
&gt; &gt; &gt; substituted by an iteration of dividing self by all even numbers starting<br>
&gt; &gt; &gt; from 3<br>
&gt; &gt; &gt; and (correctly) stopping at self sqrtFloor.<br>
&gt; &gt; &gt; This is IMHO a questionable/useless &quot;improvement&quot;, not even looking to<br>
&gt; &gt; try<br>
&gt; &gt; &gt; to implement the Sieve of Eratostene...!<br>
&gt; &gt; &gt;<br>
&gt; &gt; &gt; Again IMHO isPrime should be reverted back to what has been renamed<br>
&gt; &gt; &gt; isProbablyPrime<br>
&gt; &gt; &gt;<br>
&gt; &gt; &gt; Not being anymore used to contribute I just signal it here...<br>
&gt; &gt; &gt;<br>
&gt; &gt; &gt; Hope it helps<br>
&gt; &gt; &gt; Bye<br>
&gt; &gt; &gt; --<br>
&gt; &gt; &gt; Enrico Spinielli<br>
&gt; &gt;<br>
&gt; &gt; Enrico,<br>
&gt; &gt;<br>
&gt; &gt; Is this your original implementation?<br>
&gt; &gt;<br>
&gt; &gt;<br>
&gt; &gt;   isPrime<br>
&gt; &gt;        &quot;See isProbablyPrimeWithK:andQ: for the algoritm description.&quot;<br>
&gt; &gt;        | k q |<br>
&gt; &gt;        self &lt;= 1 ifTrue: [^self error: &#39;operation undefined&#39;].<br>
&gt; &gt;        self even ifTrue: [^self = 2].<br>
&gt; &gt;        k := 1.<br>
&gt; &gt;<br>
&gt; &gt;        q := self - 1 bitShift: -1.<br>
&gt; &gt;        [q odd] whileFalse:<br>
&gt; &gt;                        [q := q bitShift: -1.<br>
&gt; &gt;                        k := k + 1].<br>
&gt; &gt;<br>
&gt; &gt;        25 timesRepeat: [(self isProbablyPrimeWithK: k andQ: q) ifFalse:<br>
&gt; &gt; [^false]].<br>
&gt; &gt;        ^true<br>
&gt; &gt;<br>
&gt; &gt; It was recently changed as follows:<br>
&gt; &gt;<br>
&gt; &gt; &gt; Name: Kernel-ul.305<br>
&gt; &gt; &gt; Author: ul<br>
&gt; &gt; &gt; Time: 25 November 2009, 2:55:43.339 am<br>
&gt; &gt; &gt; UUID: a95be01c-d87c-154b-bdc6-c582dafad80b<br>
&gt; &gt; &gt; Ancestors: Kernel-nice.304<br>
&gt; &gt; &gt;<br>
&gt; &gt; &gt; - added Integer &gt;&gt; #sqrtFloor, which returns the floor of the square root<br>
&gt; &gt; of the receiver.<br>
&gt; &gt; &gt; - renamed Integer &gt;&gt; #isPrime to #isProbablyPrime.<br>
&gt; &gt; &gt; - added Integer &gt;&gt; #isPrime which is implemented as a deterministic<br>
&gt; &gt; primality test<br>
&gt; &gt; &gt; - both #isPrime and #isProbablyPrime return false for receivers &lt;= 1<br>
&gt; &gt; instead of raising an error<br>
&gt; &gt;<br>
&gt; &gt; Is this a reasonable change?<br>
&gt; &gt;<br>
&gt; &gt; Dave<br>
&gt; &gt;<br>
&gt; &gt;<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>