* * Little Smalltalk, * Prime number generator (modified from Tim Budd's book) * Written by David H. Lorenz, Technion, May 1993 * Ver. 1.01 * * to use, first file in this file, then create an new object of * one the prime classes and play with it by sending first and next messages * * File new; fileIn: 'primes.st' * p <- Prime1 new * p first * p next * p next * ... * p <- Prime2 new * p first * p next * ... * p <- Prime4 new * p first * p next * ... * p <- Prime5 new * p first * p next * ... * * Known bugs: Prime3 loops for ever, left as a quize for the students... Class Interval Collection lower upper step current Class Prime1 Dictionary prev last Class Prime2 Dictionary prev last Class Prime3 Dictionary last Class FactorFilter Dictionary myFactor generator Class Prime4 Dictionary gen last Class AbsGen Dictionary firstB nextB Class Prime5 Prime4 gen last Methods Interval inRange: value (step > 0) ifTrue: [ value <= upper ] ifFalse: [ value >= upper ] | first current <- lower. ^ (self inRange: current) ifTrue: [ current ] | next current <- current + step. ^ (self inRange: current) ifTrue: [ current ] | do: aBlock | item | item <- self first. [item notNil] whileTrue: [ aBlock value: item. item <- self next ]. ^ nil ] Methods Prime1 first prev <- Set new. prev add: (last <- 2). ^ last | next [ last <- last + 1. self test: last] whileTrue. prev add: last. ^ last | test: n prev do: [:x | (n\\x=0) ifTrue: [ ^ true ]]. ^ false ] Methods Prime2 first prev <- List new. prev add: (last <- 2). ^ last | next [ last <- last + 1. self test: last] whileTrue. prev addLast: last. ^ last | test: n prev do: [:x | (x squared > n) ifTrue: [ ^ false ]. (n\\x=0) ifTrue: [ ^ true ]] ] Methods Prime3 first ^ last <- 2 | next [ last <- last + 1. self test: last] whileTrue. ^ last | test: n (Prime3 new) do: [:x | (x squared > n) ifTrue: [ ^ false ]. (n\\x=0) ifTrue: [ ^ true ]] ] Methods FactorFilter remove: f from: g myFactor <- f. generator <- g | next |possible| [(possible <- generator next) notNil] whileTrue: [(possible \\ myFactor ~= 0) ifTrue: [ ^ possible ]]. ^ nil ] Methods Prime4 first gen <- 2 to: 100. last <- gen first. ^ last | next gen <- (FactorFilter new; remove: last from: gen). ^ last <- gen next ] Methods AbsGen firstBlock: b1 nextBlock: b2 firstB <- b1. nextB <- b2 | first ^ firstB value | next ^ nextB value ] Methods Prime5 collect: collectB |testB| testB <- [:x | (x notNil) ifTrue: [ collectB value: x ]]. ^ AbsGen new; firstBlock: [ testB value: self first ] nextBlock: [ testB value: self next ] ] Methods Prime5 reject: selectB |testB result| testB <- [:x | result <- x. [(result notNil) and: [(selectB value: result)]] whileTrue: [result <- self next]. result]. ^ AbsGen new; firstBlock: [testB value: self first] nextBlock: [testB value: self next] ] Methods Interval dot: buildB with: secGen |y testB| testB <- [:x :y | ((x notNil) and: [y notNil]) ifTrue: [buildB value: x value: y]]. ^ AbsGen new; firstBlock: [testB value: self first value: secGen first] nextBlock: [testB value: self next value: secGen next] ]