Sieve of Eratosthenes: Difference between revisions

Content deleted Content added
Line 1,702: Line 1,702:
====using 'easy way' to 'add' 2n wheels====
====using 'easy way' to 'add' 2n wheels====
{{trans|ZX Spectrum Basic}}
{{trans|ZX Spectrum Basic}}
Sets h$ to 1 for higher multiples of 2 via <code>FILL$</code>, later on sets <code>STEP</code> to 2n; replaces Floating Pt array p(z) with string variable h$(z) to sieve out all primes < z=441 (<code>l</code>=21) in under 1K, so that h$ is fillable to its maximum (32766), even on a 48K ZX Spectrum if translated back.
Sets h$ to 1 for higher multiples of 2 via <code>FILL$</code>, later on augments each <code>STEP</code> accordingly; replaces Floating Pt array p(z) with string variable h$(z) to sieve out all primes < z=441 (<code>l</code>=21) in under 1K, so that h$ is fillable to its maximum (32766), even on a 48K ZX Spectrum if translated back.
<lang qbasic>
<lang qbasic>
10 INPUT "Enter Stopping Pt for squared factors: ";z
10 INPUT "Enter Stopping Pt for squared factors: ";z
15 LET l=SQR(z)
15 LET l=SQR(z)
20 LET h$="10" : h$=h$ & FILL$("01",z)
20 LET h$="10" : h$=h$ & FILL$("01",z)
40 FOR n=3 TO l
40 FOR n=3 TO l STEP 2
50 IF h$(n) THEN NEXT n
50 IF h$(n) THEN NEXT n
60 FOR k=n*n TO z STEP n+n: h$(k)=1
60 FOR k=n*n TO z STEP n+n: h$(k)=1
Line 1,716: Line 1,716:


====2i wheel emulation of Sinclair ZX81 BASIC====
====2i wheel emulation of Sinclair ZX81 BASIC====
Backward-compatible also on Spectrums, as well as 1K ZX81s for all primes < Z=441. N.B. line 40 adds a <code>STEP</code> of 2 to mitigate line 50's inefficiency when going to 90. <lang qbasic>
Backward-compatible also on Spectrums, as well as 1K ZX81s for all primes < Z=441. N.B. the <code>STEP</code> of 2 in line 40 mitigates line 50's inefficiency when going to 90. <lang qbasic>
10 INPUT Z
10 INPUT Z
15 LET L=SQR(Z)
15 LET L=SQR(Z)