Factors and Prime Number

See all articles from ZX-Appeal Apr 87
Author(s): Rusty Townsend
Publication: ZX-Appeal
Pub Details: ZX-Appeal Apr 87
Date: April 1987
Pages: 15

Here are two programs that might be of interest to those of you who like to play around with numbers.

I wrote the first program, FACTORS, back in 1982 to use on my TS1000. It will print out all the integer factors of a given number, or show that the given number is a prime number.

I recently acquired a TS2068 and, considering all its improvements over the TS1000, expected that it would calculate math problems much faster. I was surprised to find that it is actually slower than the TS1000!

The number 1111111 is the product of 239 qnd 4649, both of which are prime numbers. The TS1000, in FAST, will print out those two factors in approximately 2 minutes and 15 seconds. The TS2068 takes a full 3 minutes to do the same thing!

I wrote the second program, PRIMES, to use on my recently-acquired TS2068. It will print out all the prime numbers in a given range of numbers. I no longer have a TS1000 to do a comparision, but I can tell you that the TS2068 takes 1 minute and 10 seconds to show that 1009 is the only prime number from 1000 to 1010.

Obviously I could save some time if both programs were written or compiled into machine code. Not having learned machine code yet, I have considered getting a compiler like TIME MACHINE. However, I am one of those who have ordered a QL kit, and until I get it assembled and running,I am reluctant to order anything else. (I have also put off ordering an interface to connect this TS2068 to my idle Roland printer.)

If some reader can put these programs into machine code, particularly via TIME MACHINE, I would be interested to hear how much faster the compiled programs run the examples I’ve given above.

Program: FACTORS

10 INPUT N
20 PRINT N
30 LET D=2
40 LET N=N/D
50 IF INT N=N THEN PRINT D,N
60 IF INT N=N THEN GO TO 40
70 LET N=INT (N*D+.1)
80 LET D=D+1
90 IF D>N THEN PRINT D-1;" IS THE FINAL PRIME FACTOR."
100 IF D>N THEN STOP
110 GO TO 40

Program: PRIMES

5 REM PRIME NUMBER GENERATOR
10 INPUT A
20 INPUT B
30 PRINT "THE PRIME NUMBERS BETWEEN "
40 PRINT A;" AND ";B;" ARE -"
50 FOR C=A TO B
60 LET N=C
70 LET D=2
80 LET N=N/D
90 IF INT N=N THEN GO TO 80
100 LET N=INT (N*D+.1)
110 LET D=D+1
120 IF D<N THEN GO TO 80
130 IF D>N THEN GO TO 150
140 IF D=C THEN PRINT ,D
150 NEXT C

Note: Type-in program listings on this website use ZMAKEBAS notation for graphics characters.