Sieve for the sinclair ZX Spectrum

Anything you want to discuss about your vintage computer, or accessories for it, from general usage to advanced modding.

Sieve for the sinclair ZX Spectrum

Postby Trygve » Sun Oct 09, 2005 9:20 pm

For those living in America, this is a Timex Sinclair (2000?) 8bit computer.

This version of the program is slightly optimized compared to the Atari ST version it is based on...

Code: Select all
10 DIM a(255): LET k=2
20 FOR n=2 TO 255/k
30 LET a(n*k)=1
40 NEXT n
50 FOR n=K+1 TO 255
60 IF a(n)=1 THEN GO TO 80
70 LET k=n: LET n=255
80 NEXT n
90 IF k < 128 THEN GO TO 20

This program seems to take about 8 seconds to run...
Afterwards the array a() will contain a '0' in all locations corresponding to a prime number.

I have not yet attempted a 16bit version...

For anyone wanting to get the numbers on the screen, add these lines to the program before running it:

Code: Select all
100 FOR n=1 TO 255: IF a(n)=0 THEN PRINT n
110 NEXT n

Can this be optimized further?
User avatar
Über Geek
Posts: 108
Joined: Sun Jun 19, 2005 9:49 pm
Location: Norway

Return to General computer talks

Who is online

Users browsing this forum: No registered users and 1 guest