-
Notifications
You must be signed in to change notification settings - Fork 1
/
p7.st
49 lines (38 loc) · 972 Bytes
/
p7.st
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
Object subclass: #Primes
instanceVariableNames: 'n '
classVariableNames: ''
poolDictionaries: ''
category: 'Project-Euler'!
!Primes commentStamp: 'JK 3/23/2024 11:51' prior: 0!
Prime number functions!
!Primes methodsFor: 'initialize-release' stamp: 'JK 3/23/2024 12:28'!
initialize
n := 1! !
!Primes methodsFor: 'accessors' stamp: 'JK 3/23/2024 12:24'!
n
^n! !
!Primes methodsFor: 'generators' stamp: 'JK 3/23/2024 12:28'!
next
n := n + 1.
[(Primes isPrime: n) not] whileTrue: [n := n + 1].
^ n! !
"-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- "!
Primes class
instanceVariableNames: ''!
!Primes class methodsFor: 'checkers' stamp: 'JK 3/23/2024 12:19'!
isPrime: n
| m |
m := 2.
[(m * m) <= n] whileTrue: [
((n \\ m) = 0) ifTrue: [ ^ false ].
m := m + 1.
].
^true! !
!Primes class methodsFor: 'functions' stamp: 'JK 3/23/2024 12:32'!
nthPrime: n
| p |
p := Primes new.
p initialize.
(n - 1) timesRepeat: [p next.].
^(p next)
! !