# Ulam number spiral

If all integers are laid out, spiralling out from the centre, and the primes are high-lit, they seem to cluster more densely on certain diagonals. No-one know why. See wikipedia....

It is arguable whether to start at zero or 1; and whether to show 0 and 1. Have fun experimenting!

It's a nice exercise in programming.... my version allows you to study centrral area, or a wider one, and to speed up or slow down. At present it does not offer to write in the numbers or 'join the dots'.
```

'   Ulam spiral primes pattern

'   Uses Eratosthenes' sieve to generate a look-up string of primes first

nomainwin

UpperLeftX   =   2
UpperLeftY   =  10
WindowWidth  = 660
WindowHeight = 740

graphicbox #w.g,       10,  10, 640, 640
statictext #w.st,  "", 10, 655, 400,  40
button     #w.b,  "Quit",  [quit], LR,  50,  20,  40,  20
button     #w.b2, "Fast",  fast,   LR, 120,  20,  40,  20

open "Stanislav Ulam- Prime Spiral." for graphics_nf_nsb as #w

#w   "trapclose [quit]"
#w.g "goto 320 320 ; down ; fill black ; size 2 ; flush"
#w.st "Running!"
#w.b2 "!disable"

global pi, prime\$, sleep
pi   =  3.14159265359

dir    =   0
x      = 320
y      = 320
pace   =   3
SqSide = 100
c      =   1
i      =   0
sleep  =1000

#w.st "Listing primes and saving for look-up. Please wait a few seconds."
topinteger =SqSide^2
dim ints( topinteger)

for j =1 to topinteger                      '   set all integer to be flagged as '1' ie possibly prime
ints( j) =1
next j

for j =2 to 1 +topinteger^0.5
if ints( j) =1 then gosub [strikeout]   '   If exists, is prime. Strike out its multiples.
next j                                      '   Makes no assumptions eg re even numbers.

prime\$ ="000001/"                                  '   Create a single long string holding all primes and a separator ( '/')
for j =2 to topinteger
if ints( j) =1 then prime\$ =prime\$ +right\$( "000000" +str\$( j), 6) +"/"
next j

'open "rand.txt" for output as #r
'    #r prime\$;
'close #r

#w.b2 "!enable"

#w.st "Spiralling out!"
do
for s =1 to c
x =x +pace *cos( dir)
y =y +pace *sin( dir)
i =i +1
'#w.st i; " ";
calldll #kernel32, "Sleep", sleep as ulong, res as void
if check( i) =1 then #w.g "color black" else #w.g "color white"
#w.g "set "; x; " "; y
scan
next s
dir =dir +pi /2

if c >SqSide then exit do
for s =1 to c
x =x +pace *cos( dir)
y =y +pace *sin( dir)
i =i +1
'#w.st i; " ";
calldll #kernel32, "Sleep", sleep as ulong, res as void
if check( i) =1 then #w.g "color black" else #w.g "color white"
#w.g "set "; x; " "; y
scan
next s
dir =dir +pi /2

c =c +1

loop until 1 <>1

#w.g "flush"

wait

sub fast a\$
if sleep = 1 then
sleep =1000
#w.b2 "Fast"
else
sleep =1
#w.b2 "Slow"
end if
end sub

function check( i)
if instr( prime\$, right\$( "000000" +str\$( i), 6) +"/") then check =1: #w.st i; " is PRIME" else check =0: #w.st i
end function

[strikeout]
for k =2 *j to topinteger step j
ints( k) =0
next k
return

[quit]
close #w
end

```