[Cuis-dev] Problems in class Number

Andres Valloud ten at smallinteger.com
Thu Oct 10 21:40:35 PDT 2019


Fascinating --- the gcd approach was pretty bad in 32 bit land. 
However, in 64 bits, the gcd batches are large enough to amortize the 
cost, and that detects most composites without sending sqrtFloor.  The 
small integer threshold is now 91 squared.

On 10/10/19 20:37, Agustín Sansone via Cuis-dev wrote:
>     Well, what do you think?  Are we done going over this poor method?
> 
> 
> Okay, I'm happy with this version.
> 
-------------- next part --------------
'From Cuis 5.0 [latest update: #3866] on 10 October 2019 at 9:29:31 pm'!

!Integer methodsFor: 'testing' stamp: 'sqr 10/10/2019 21:23:47'!
isPrimeFast2i

	self < 3 ifTrue: [^self = 2].
	self even ifTrue: [^false].
	self \\ 3 = 0 ifTrue: [^self = 3].
	self \\ 5 = 0 ifTrue: [^self = 5].
	self < 8281 ifTrue: [
		"Approximate sqrtFloor to avoid computational expense"
		self \\ 7 = 0 ifTrue: [^self = 7].
		self \\ 11 = 0 ifTrue: [^self = 11].
		self \\ 13 = 0 ifTrue: [^self = 13].
		12 to: (self bitShift: -6) + 11 by: 6 do:
			[:each |
				self \\ (each+5) = 0 ifTrue: [^false].
				self \\ (each+7) = 0 ifTrue: [^false]
			].
		^true
	].
	"Now 2, 3 and 5 do not divide self. So, self is of the form
	30*k + {1, 7, 11, 13, 17, 19, 23, 29} for integer k >= 0.
	These gcd operations test all relevant primes less than 97,
	and in particular less than 91^2 = 8281"
	self gcd: 20496326086283047 :: = 1 ifFalse: [^false].
	self gcd: 38655288426304091 :: = 1 ifFalse: [^false].
	"Bad luck, must incur the expense of sqrtFloor.
	The 31 case below is the 30k+1 case, excluding k = 0"
	90 to: self sqrtFloor by: 30 do: [:each |
		self \\ (each+7) = 0 ifTrue: [^false].
		self \\ (each+11) = 0 ifTrue: [^false].
		self \\ (each+13) = 0 ifTrue: [^false].
		self \\ (each+17) = 0 ifTrue: [^false].
		self \\ (each+19) = 0 ifTrue: [^false].
		self \\ (each+23) = 0 ifTrue: [^false].
		self \\ (each+29) = 0 ifTrue: [^false].
		self \\ (each+31) = 0 ifTrue: [^false].
	].
	^true! !


More information about the Cuis-dev mailing list