Andreas Raab uploaded a new version of Kernel to project The Trunk:
http://source.squeak.org/trunk/Kernel-ul.305.mcz==================== Summary ====================
Name: Kernel-ul.305
Author: ul
Time: 25 November 2009, 2:55:43 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
=============== Diff against Kernel-nice.304 ===============
Item was added:
+ ----- Method: Integer>>isProbablyPrime (in category 'testing') -----
+ isProbablyPrime
+ "See isProbablyPrimeWithK:andQ: for the algoritm description."
+
+ | k q |
+ self <= 1 ifTrue: [ ^false ].
+ 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!
Item was added:
+ ----- Method: Integer>>sqrtFloor (in category 'mathematical functions') -----
+ sqrtFloor
+ "Return the integer part of the square root of self"
+
+ | guess guessSquared delta |
+ guess := 1 bitShift: self highBit + 1 // 2.
+ [
+ guessSquared := guess * guess.
+ delta := guessSquared - self // (guess bitShift: 1).
+ delta = 0 ] whileFalse: [
+ guess := guess - delta ].
+ guessSquared = self ifFalse: [ guess := guess - 1 ].
+ ^guess!
Item was changed:
----- Method: Integer>>isPrime (in category 'testing') -----
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].
+ self <= 1 ifTrue: [ ^false ].
+ self even ifTrue: [ ^self = 2].
+ 3 to: self sqrtFloor by: 2 do: [ :each |
+ self \\ each = 0 ifTrue: [ ^false ] ].
- 25 timesRepeat: [(self isProbablyPrimeWithK: k andQ: q) ifFalse: [^false]].
^true!