Fwd: Sieve of Eratosthenes
Posted by Gil Egozi on Oct 10, 2006; 4:01am
URL: https://forum.world.st/Fwd-Sieve-of-Eratosthenes-tp56093.html
Hi,
I am resending this since i wan't a member, and the message is still somewhere in moderation-land.
after looking at Ramon's version i noticed the java version isn't returning the primes, so we can remove the last select:
this cuts time in half down close to: 500
Integer>>sieveOfEratosthenes
"generates primes up to self, educational algorithm, not for
production use"
| primes |
primes := ByteArray new: self.
2 to: self do: [:each |
(primes at: each) = 0 ifTrue: [each + each to: self
by: each do: [:notPrime |
primes at: notPrime put: 1]]].
"-------------"
SmalltalkImage current snapshot: true andQuit: false
Smalltalk garbageCollect.
Smalltalk garbageCollect.
Smalltalk garbageCollect. "force full garbage collect"
Time millisecondsToRun: [1000000 sieveOfEratosthenes].
Gil
--
Many people would sooner die than think; In fact, they do so.
-- Bertrand Russell