[squeak-dev] Integer>>isPrime why reverting Knuth's algorithm P (probabilistic primality test) with iteration over division with even numbers?

David T. Lewis lewis at mail.msen.com
Fri Dec 18 23:56:47 UTC 2009


On Fri, Dec 18, 2009 at 05:25:45PM +0100, Enrico Spinielli wrote:
> Hi all,
> I am back to checking Squeak after quite a while and got latest trunk.
> I looked after one of my contributions Integer>>isPrime
> and I found my implementation of Algorithm P from Knuth's AOCP vol 2
> substituted by an iteration of dividing self by all even numbers starting
> from 3
> and (correctly) stopping at self sqrtFloor.
> This is IMHO a questionable/useless "improvement", not even looking to try
> to implement the Sieve of Eratostene...!
> 
> Again IMHO isPrime should be reverted back to what has been renamed
> isProbablyPrime
> 
> Not being anymore used to contribute I just signal it here...
> 
> Hope it helps
> Bye
> -- 
> Enrico Spinielli

Enrico,

Is this your original implementation?


   isPrime
	"See isProbablyPrimeWithK:andQ: for the algoritm description."
	| k q |
	self <= 1 ifTrue: [^self error: 'operation undefined'].
	self even ifTrue: [^self = 2].
	k := 1.

	q := self - 1 bitShift: -1.
	[q odd] whileFalse: 
			[q := q bitShift: -1.
			k := k + 1].
	
	25 timesRepeat: [(self isProbablyPrimeWithK: k andQ: q) ifFalse: [^false]].
	^true

It was recently changed as follows:

> Name: Kernel-ul.305
> Author: ul
> Time: 25 November 2009, 2:55:43.339 am
> UUID: a95be01c-d87c-154b-bdc6-c582dafad80b
> Ancestors: Kernel-nice.304
> 
> - added Integer >> #sqrtFloor, which returns the floor of the square root of the receiver.
> - renamed Integer >> #isPrime to #isProbablyPrime.
> - added Integer >> #isPrime which is implemented as a deterministic primality test
> - both #isPrime and #isProbablyPrime return false for receivers <= 1 instead of raising an error

Is this a reasonable change?

Dave

 



More information about the Squeak-dev mailing list