I was thinking of a lookup table with 1000 boolean entries with trues for the prime number entries
32 DINTs, with 32-bits each, can represent the primality of all natural numbers from 0 to 1023; see below. Each DINT represents 32 of those natural numbers, e.g. bit kprime_dint_array[1].2 represents the natural number 33; each bit either is 1 if its corresponding natural number is prime, or is 0 if its corresponding natural number is composite.
Writing the AOI that takes a natural number and returns the corresponding bit from the array is left as an exercise for the student.
$ cat kprime.py
"""Sieve of Eratosthenes"""
import os
import struct
### Set all bits as prime for natural numbers 0-1023
primebits = [0xFFFFFFFF]*32
### function to convert natural number to index and mask into primebits
imask_idx = lambda ival: (1 << (ival & 31),ival >> 5,)
### Clear prime bits for natural number 0 and 1
for ii in [0,1]:
imask,intno = imask_idx(ii)
primebits[intno] ^= imask
if 'DEBUG' in os.environ: print(f"Removed {(ii,hex(imask),intno,)}")
### Clear bits for natural numbers caught in the Sieve of Eratosthenes
for i in range(2,33,1):
imask,intno = imask_idx(i)
if 0==(primebits[intno] & imask): continue
for ii in range(i*i,1024,i):
imask,intno = imask_idx(ii)
if (primebits[intno] & imask):
if 'DEBUG' in os.environ: print(f"Removed {(ii,hex(imask),intno,)}")
primebits[intno] ^= imask
### Convert from unsigned to signed 32-bit integers, and output results
print(struct.unpack('i'*32,struct.pack('I'*32,*primebits)))
$ python kprime.py | tee x.x | tr , \\n | tr -d ' ()' ; cat x.x
32 DINTs in a column, hopefully suitable for pasting into Studio 5k:-1601558356
673221152
34112136
-2147342174
547359240
-2144853880
-2146959198
134382120
169910402
579338272
671221760
-2011135870
33693728
142770216
-2146917886
545785984
536873472
168304648
-2105407486
8391200
2629770
537397282
545292320
35684488
537395234
681576960
-1977614208
9052160
8423424
34087426
-2138961792
673316896
32-bit DINTs comma-separated on one line:(-1601558356, 673221152, 34112136, -2147342174, 547359240, -2144853880, -2146959198, 134382120, 169910402, 579338272, 671221760, -2011135870, 33693728, 142770216, -2146917886, 545785984, 536873472, 168304648, -2105407486, 8391200, 2629770, 537397282, 545292320, 35684488, 537395234, 681576960, -1977614208, 9052160, 8423424, 34087426, -2138961792, 673316896)
E.g 673221152, the second number, representing 32-63:
Code:
63 32
| |
v v
2#001[COLOR=Blue][B]0[/B][/COLOR]10000[COLOR=blue][B]0[/B][/COLOR]10000[COLOR=blue][B]0[/B][/COLOR]10001[COLOR=blue][B]0[/B][/COLOR]10001[COLOR=blue][B]0[/B][/COLOR]0000
^ ^ ^ ^ ^ ^ ^
| | | | | | |
| 59 53 47 | 41 37
61 43
N.B. 6n values are highlighted in
bold blue