[Cuis-dev] Problems in class Number

Andres Valloud ten at smallinteger.com
Thu Oct 10 19:34:54 PDT 2019


Hmmm, in here,

	self < 384 ifTrue: [
		"Approximate sqrtFloor to avoid computational expense"
		3 to: (self bitShift: -3) + 1 by: 2 do:
			[:each | self \\ each = 0 ifTrue: [ ^false ]].
		^true
	].


the loop should start at 7 because the factors 3 and 5 have already been 
excluded.  This gains noticeable speed for smallish integers (because 
384 sqrtFloor is just 19).  Looking into this further led to another 30% 
speed increase.  Would you mind checking?

On 10/10/19 18:40, Agustín Sansone via Cuis-dev wrote:
>     Here's something really important: doing too much micro-optimization
>     detracts from the motivation to implement better things.  I'd say we
>     leave things with the round of 30.
> 
> 
> Okay. This is the 30*k version.
> 
-------------- next part --------------
'From Cuis 5.0 [latest update: #3866] on 10 October 2019 at 7:33:03 pm'!

!Integer methodsFor: 'testing' stamp: 'sqr 10/10/2019 19:32:23'!
isPrimeFast2f

	self < 3 ifTrue: [ ^self = 2 ].
	self even ifTrue: [^false].
	self \\ 3 = 0 ifTrue: [^self = 3].
	self \\ 5 = 0 ifTrue: [^self = 5].
	self < 1522 ifTrue: [
		"Approximate sqrtFloor to avoid computational expense"
		self \\ 7 = 0 ifTrue: [^self = 7].
		11 to: (self bitShift: -5) + 8 by: 2 do:
			[:each | self \\ each = 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.
	The 31 case below is the 30k+1 case, excluding k = 0"
	0 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