-
Notifications
You must be signed in to change notification settings - Fork 570
/
Copy pathsieve_xharbour.prg
222 lines (169 loc) · 6.62 KB
/
sieve_xharbour.prg
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
*±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±
* Procedure SIEVE_XHARBOUR.PRG
*
* Dave's Garage Prime Sieve Speed Test Algorithm For Different
* Computer Languages
*
* Development Languge : XHarbour
*
* Authors : Andy Radford, Bradley Chatha
*
* Date : 20/9/2021 (DD/MM/CCYY)
*
*
*±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±
*
*
* Notes:
* 1. XHarbour only version due to it bypassing the 4096 array element limit
*
* 2. No Additional Libraries have been used eg. (CA-Tools)
*
* 3. If runtime exceeds 1 day, the duration will be eroneous
*
* 4. The sieve size is determined by the SieveSize variable (currently
* set to 1000000
*
* 5. Clipper's array element size is 14 bytes
*
*
*±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±
*
* FUNCTIONS AND PROCEDURES
*
* PROCEDURE Main()
* FUNCTION ReferenceN()
* FUNCTION RunSieve()
* FUNCTION InitArray()
* PROCEDURE SetElement()
* FUNCTION GetElement()
*
*±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±
Main()
*±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±
* PROCEDURE Main()
*±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±
*
PROCEDURE Main()
LOCAL SieveSize := 1000000
LOCAL PassCount := 0
LOCAL StartTime := SECONDS()
LOCAL FinishTime := 0
LOCAL Duration := 0
LOCAL NumberFound := 0
LOCAL Now := Seconds()
LOCAL OutputString := ""
DO WHILE Now - StartTime <= 5
NumberFound = RunSieve(SieveSize)
PassCount = Passcount + 1
Now = SECONDS()
ENDDO
//CA Clipper 5.2e SECOND() Function returns number of seconds since midnight
//0-86399
//
//So rather than end up with a negative duration (which would be unfair on
//the competitors and time travel is not yet possible, I will add 86399
//(seconds in a day) to the finish time if the duration works out as
//negative
//
//Limition : This does not consider the possibility of it running multiple
//Days
FinishTime = SECONDS()
Duration = FinishTime - StartTime
IF Duration < 0
FinishTime = FinishTime + 86399
Duration = FinishTime - StartTime
ENDIF
IF NumberFound <> ReferenceN(SieveSize)
? "WARNING: result is incorrect!"
ENDIF
OutputString = "AndyRadford,BradleyChatha,XH"
OutputString = OutputString + ";" + ALLTRIM(STR(PassCount))
OutputString = OutputString + ";" + ALLTRIM(STR(Duration))
OutputString = OutputString + ";" + ALLTRIM(STR(1))
OutputString = OutputString + ";" + ALLTRIM("algorithm=base")
OutputString = OutputString + "," + ALLTRIM("faithful=no")
OutputString = OutputString + "," + ALLTRIM("bits=112")
? OutputString
RETURN
*±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±
* FUNCTION ReferenceN()
*±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±
*
FUNCTION ReferenceN(SieveSize)
LOCAL Primecounts := {{10,4},{100,25},{1000,168},{10000,1229},{100000,9592},{1000000,78498},{10000000,664579},{100000000,5761455}}
LOCAL ReferenceCount := 0
FOR Looop = 1 TO LEN(PrimeCounts)
IF PrimeCounts[looop,1] = SieveSize
ReferenceCount = PrimeCounts[Looop,2]
EXIT
ENDIF
NEXT
RETURN (ReferenceCount)
*±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±
* FUNCTION RunSieve()
*±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±
*
FUNCTION RunSieve (SieveSize)
LOCAL ResultCount := 0
LOCAL SieveSqrt := SQRT(SieveSize)
LOCAL Number := 0
LOCAL Factor := 0
LOCAL PArray := (SieveSize+1)/2
LOCAL PrimesArrayCL := {}
PrimesArrayCL := InitArray(PArray,.F.)
FOR Factor = 3 TO SieveSqrt Step 2
FOR Number = Factor TO SieveSqrt STEP 2
IF GetElement(PrimesArrayCL,INT(Number / 2)) = .F.
Factor = Number
EXIT
ENDIF
NEXT Number
IF Number > SieveSqrt
EXIT
ENDIF
FOR Number = Factor * 3 TO SieveSize STEP Factor * 2
SetElement(PrimesArrayCL,INT(Number / 2),.T.)
NEXT
NEXT
//Count the number of Primes (inverted logic warning)
FOR Counter = 1 TO PArray
IF GetElement(PrimesArrayCL,Counter) = .F.
ResultCount = ResultCount + 1
ENDIF
NEXT
RETURN (ResultCount)
*±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±
* FUNCTION InitArray()
*±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±
*
FUNCTION InitArray(ElementCount,initvalue)
LOCAL array := ARRAY(ElementCount)
FOR I = 1 TO ElementCount
array[I] := initvalue
NEXT
RETURN (array)
*±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±
* PROCEDURE SetElement()
*±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±
*
PROCEDURE SetElement(Array, Index, Value)
// Support of setting array element when the array size > 4096 elements
Array[Index] := Value
RETURN
*±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±
* FUNCTION GetElement()
*±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±
*
FUNCTION GetElement(Array, Index)
RETURN (Array[Index])
***
* VERSION HISTORY :
* 1.0 : Initial Release (20/09/2021 ACR)
* Credit Jovan Bulajic, Yogoslavia :
* Solution to Clippers 4096 single array limit
* Concept of Multi-Dimensional Array
* 1.1 : Correct output (22/09/2021 ACR)
* 1.2 : Refactored into XHarbour version (22/09/2021 BPAC)
*
* EoF: SIEVE_XHARBOUR.PRG