=========================================================================== BBS: The Abacus * HST/DS * Potterville MI Date: 05-08-93 (14:23) Number: 13 From: DOUGLAS LUSHER Refer#: NONE To: QUINN TYLER JACKSON Recvd: NO Subj: IMPROVEMENT Conf: (35) Quick Basi --------------------------------------------------------------------------- 'Mr. Jackson, I sent this message to you about 10 days ago 'and have not received any response or acknowledgement, and 'my source for this echo has a way of losing mail, so I'm 'sending it again. I was looking at the code for the routine 'funFirstPrime%() which I believe is part of the QB-CIMR 'project, and I made what I believe are some improvements. 'I have called my routine HashTableSize%(), but if you wish 'to make use of it, you can call it whatever you want, of 'course. Here is some code to compare the two routines: DECLARE FUNCTION HashTableSize% (Threshold%) DECLARE FUNCTION funFirstPrime% (Threshold%) DEFINT A-Z: X% = 0: Y% = 0: Z% = 0 PRINT "Timing comparison in progress..." RANDOMIZE 12345 S! = TIMER: DO: S1! = TIMER: LOOP WHILE S! = S1! FOR Z% = 1 TO 1000 X% = 3 + INT(RND * 16380) Y% = HashTableSize%(X%) NEXT E1! = TIMER: T1! = E1! - S1! RANDOMIZE 12345 S! = TIMER: DO: S2! = TIMER: LOOP WHILE S! = S2! FOR Z% = 1 TO 1000 X% = 3 + INT(RND * 16380) Y% = funFirstPrime%(X%) NEXT E2! = TIMER: T2! = E2! - S2! PRINT "Time for HashTableSize:"; T1! PRINT "Time for funFirstPrime:"; T2! 'and the following code shows that the two function give the same 'output over the input range of 3 to 16383. The funFirstPrime 'routine does not appear to me to give the correct output 'for inputs of 1 and 2 - but perhaps it's irrelevant at that 'small of size. 16383 was the upper limit for the original 'routine, so I made mine identical in that respect, but the 'upper limit could be much higher with just a slight adjustment 'that would incure only a very small speed penalty. PRINT "Output comparison in progress..." FOR Threshold% = 3 TO 16383 IF HashTableSize%(Threshold%) <> funFirstPrime(Threshold%) THEN BEEP: PRINT "Error found for Threshold of:"; Threshold%: STOP END IF NEXT FUNCTION funFirstPrime (Threshold) tp30 = INT((Threshold * 1.3) + .5) IF tp30 / 2 = tp30 \ 2 THEN tp30 = tp30 + 1 END IF C = tp30 - 2 IF C < 1 THEN C = 1 END IF T2 = Threshold * 2 DO C = C + 2 FOR Z = 3 TO SQR(C) ind = -1 IF C / Z = C \ Z THEN ind = 0 EXIT FOR END IF NEXT Z IF ind THEN IF (C - 3) / 4 = INT((C - 3) / 4) OR C > T2 THEN funFirstPrime = C EXIT DO END IF END IF LOOP END FUNCTION FUNCTION HashTableSize% (Threshold%) Lo% = INT((Threshold% * 1.3) + .5) IF (Lo% AND 1) = 0 THEN Lo% = Lo% + 1 'Lo% must be odd Hi% = Threshold% * 2 FOR X% = Lo% TO 21319 STEP 2 Prime% = -1 FOR Factor% = 3 TO INT(SQR(X%)) STEP 2 IF X% MOD Factor% = 0 THEN Prime% = 0 EXIT FOR END IF NEXT IF Prime% THEN IF ((X% - 3) AND 3) = 0 OR X% > Hi% THEN HashTableSize% = X% EXIT FOR END IF END IF NEXT END FUNCTION --- þ SLMR 2.1a þ Never let your schooling interfere with your education. --- TMail v1.31.4 * Origin: TC-AMS MLTBBS 2.2 - Minnetonka, MN (612)-938-4799 (1:282/7) SEEN-BY: 1/211 11/2 4 13/13 101/1 108/89 109/25 110/69 114/5 123/19 124/1 SEEN-BY: 153/752 154/40 77 157/2 159/100 125 430 575 950 203/23 209/209 SEEN-BY: 261/1023 280/1 390/1 396/1 15 397/2 2230/100 2440/5 3603/20