Hails!TMD2003 wrote:Hails!
Anyway, here's the listing. You know what to do with it.
Trying the program with 999983 took 34 seconds. This was using QPC2 v4.05. I was rather surprised at such a slow timing, especially since I have recently upgraded my PC to use a solid-state drive.
Looking through the code, I see line 290 FOR n=1 TO f1/2 STEP 2. As I see it, there should be no need to go further than the square root of f1. I tried this out but found that it misses the final f1 value as the final factor. So, I added f1 to the FOR loop. QL SuperBASIC does allow this. Also included are the changes suggested by Tofro and Steve. Here is the updated code …
Code: Select all
100 CLS
110 initial_input
120 factor_array(f0)
130 factors_found
140 STOP
150 REMark ==========================
160 DEFine PROCedure initial_input
170 REMark ==========================
180 PRINT "INPUT INTEGER TO FACTORISE"\"(min.2, max.999999)"\\"-> ";
190 REPeat loop1
200 INPUT f0
210 IF f0>=2 AND f0<=999999 AND f0=INT(f0) THEN EXIT loop1
220 END REPeat loop1
230 END DEFine initial_input
240 REMark ==========================
250 DEFine PROCedure factor_array(f1)
260 REMark ==========================
270 LOCal n,cf
280 DIM f(19): f1=f0
290 FOR n=1 TO SQRT(f1) STEP 2, f1
300 cf=n: IF n=1 THEN cf=2
310 AT 5,0: PRINT "Dividing by ";cf
320 REPeat loop2
330 IF cf*((f1/cf)-INT(f1/cf))<>0 THEN EXIT loop2
340 f1=f1/cf: f(f(0)+1)=cf: f(0)=f(0)+1
350 IF f1=1 THEN RETurn
360 END REPeat loop2
370 END FOR n
380 IF f1=f0 THEN f(0)=1: f(1)=f1
390 END DEFine factor_array
400 REMark ==========================
410 DEFine PROCedure factors_found
420 REMark ==========================
430 LOCal n
440 AT 5,0: PRINT "NUMBER OF FACTORS = ";f(0)\\"FACTORS:"
450 FOR n=1 TO f(0)-1: PRINT f(n)!"x ";: END FOR n: PRINT f(f(0));
460 IF f(0)=1 THEN PRINT "is a PRIME NUMBER"
470 END DEFine factors_found
By the way, I can recommend using a solid-state drive. My PC now boots up much quicker, and the long delays I have encountered in the past no longer occur.
EmmBee