Basic program for prime numbers

Anything Sinclair ZX Basic related; history, development, tips - differences between BASIC on the ZX80 and ZX81
Post Reply
Chandru arni
Posts: 1
Joined: Tue Jan 22, 2013 5:00 am

Basic program for prime numbers

Post by Chandru arni »

I got a zx81 in 1981 and did a lot of programming then.
Now I am 84 and can't remember the Basic stuff I used.
Can someone give me the full program for
"Finding the first 100 prime nos"
Thank you
dr beep
Posts: 2076
Joined: Thu Jun 16, 2011 8:35 am
Location: Boxmeer

Re: Basic program for prime numbers

Post by dr beep »

Welcome to the forum!

You can either calculate if a number can be divided by all predecessors or fill an upcoming array with all next values that can be divided by that number.
For less memory you would use the first method. For speed the second.

Most important is to test if INT (N / P) = N/P.
where N is always 1 more than the previous value and P is a loop test from 2 to N-1
If this is true then it is no prime.

This will do the first job where
F = nr found
N = number to test
P = test loop counter
REM N=2 is special, must be found too, therefore N=2 added in line 30

10 LET F=0
20 LET N=2
30 FOR P=2-(N=2) TO N-1
40 IF INT(N/P)=N/P THEN GOTO 70
50 NEXT P
60 PRINT N;" ";
70 LET F=F+1
80 LET N=N+1
90 IF F<100 THEN GOTO 30
User avatar
bbock
Posts: 54
Joined: Wed Jan 12, 2011 7:59 pm

Re: Basic program for prime numbers

Post by bbock »

Here are my 2 cents, a "sieve of Erathostenes" implementation for the ZX81, calculation the prime numbers up to 1000:

Code: Select all

10 DIM P(1000)
20 FOR I=2 TO 1000
30 LET P(I)=1
40 NEXT I
50 LET P(1)=0
60 FOR Q=2 TO 32
70 IF P(Q)=0 THEN GOTO 110
80 FOR I=Q*Q TO 1000 STEP Q
90 LET P(I)=0
100 NEXT I
110 NEXT Q
120 PRINT "FERTIG"
130 PAUSE 1000
140 LET C=0
150 FOR I=1 TO 1000
160 IF P(I)=0 THEN GOTO 240
170 IF C<8 THEN GOTO 200
180 PRINT
190 LET C=0
200 LET C=C+1
210 IF I<10 THEN PRINT " ";
220 IF I<100 THEN PRINT " ";
230 PRINT I;" ";
240 NEXT I
The code is somewhat optimized; the loop in line 60 runs to 32 only, because the square root of 1000 is approx. 31,62, thus there's no need to compute any further.

Bernd
dr beep
Posts: 2076
Joined: Thu Jun 16, 2011 8:35 am
Location: Boxmeer

Re: Basic program for prime numbers

Post by dr beep »

bbock wrote:Here are my 2 cents, a "sieve of Erathostenes" implementation for the ZX81, calculation the prime numbers up to 1000:

Code: Select all

10 DIM P(1000)
20 FOR I=2 TO 1000
30 LET P(I)=1
40 NEXT I
50 LET P(1)=0
60 FOR Q=2 TO 32
70 IF P(Q)=0 THEN GOTO 110
80 FOR I=Q*Q TO 1000 STEP Q
90 LET P(I)=0
100 NEXT I
110 NEXT Q
120 PRINT "FERTIG"
130 PAUSE 1000
140 LET C=0
150 FOR I=1 TO 1000
160 IF P(I)=0 THEN GOTO 240
170 IF C<8 THEN GOTO 200
180 PRINT
190 LET C=0
200 LET C=C+1
210 IF I<10 THEN PRINT " ";
220 IF I<100 THEN PRINT " ";
230 PRINT I;" ";
240 NEXT I
The code is somewhat optimized; the loop in line 60 runs to 32 only, because the square root of 1000 is approx. 31,62, thus there's no need to compute any further.

Bernd
And that is the other/fast approach of the program.
sirmorris
Posts: 2811
Joined: Thu May 08, 2008 5:45 pm

Re: Basic program for prime numbers

Post by sirmorris »

This is almost the fastest code available:

Code: Select all

10 PRINT "  2   3   5   7  11  13  17  19  23  29"
20 PRINT " 31  37  41  43  47  53  59  61  67  71"
30 PRINT " 73  79  83  89  97 101 103 107 109 113"
40 PRINT "127 131 137 139 149 151 157 163 167 173"
50 PRINT "179 181 191 193 197 199 211 223 227 229"
60 PRINT "233 239 241 251 257 263 269 271 277 281"
70 PRINT "283 293 307 311 313 317 331 337 347 349"
80 PRINT "353 359 367 373 379 383 389 397 401 409"
90 PRINT "419 421 431 433 439 443 449 457 461 463"
99 PRINT "467 479 487 491 499 503 509 521 523 541"
:lol: :lol: :lol: :lol: :lol:
Post Reply