Tuesday, July 16, 2024

0000 0000 1110 1011

Primed for discovery

This is not electronics but a weird combination of coding and mathematics! Young children look away now.

Some time ago I thought it might be useful to look for patterns in primes because, well, it's like a mountain to climb! If it's there then you gotta do it.

Also there has been a LOT of work done in this field before.

Here's some light reading:

https://www.nature.com/articles/nature.2016.19550

https://www.scirp.org/journal/paperinformation?paperid=74345

https://www.youtube.com/watch?v=EK32jo7i5LQ

So during a lull in my holidays (?), and after attending a math conference recently, I decided to start a coding journey that examines the position of primes in geometric patterns for both squares and hexagons.

Here's the square version:

from math import sqrt
from turtle import *

xrow = 10
ycol = 11
count = 2
squaresize = 4

tracer(False)
speed("fastest")

pendown()

def isPrime(n):
    for i in range(2,int(n**0.5)+1):
        if n%i==0:
            return False
    return True

def makesq():
    forward(squaresize)
    right(90)
    forward(squaresize)
    right(90)
    forward(squaresize)
    right(90)
    forward(squaresize)
    right(90)
    forward(squaresize)
    return    

def turn():
    right(90)
    forward(squaresize)
    return    

fillcolor("red")

begin_fill()
makesq() # 1 is not prime
end_fill()

fillcolor("blue")

while(count < 25000):
    for laying in range(1,xrow):
        if(isPrime(count)):
            begin_fill()
        makesq()
        end_fill()
        count = count + 1
    turn()
    for laying in range(1,ycol):
        if(isPrime(count)):
            begin_fill()
        makesq()
        end_fill()
        count = count + 1
    turn()
    xrow=xrow+1
    ycol=ycol+1

update()

And here is the more interesting hexagon version.

from math import sqrt
from turtle import *
import os
from turtle import Screen, Turtle

height = 900
width = 900
screen = Screen()
screen.setup(width, height)

short = 1

global count
count = 1
hexsize = 10

turnarray = [4,3,3,3,3,2,3]

#tracer(False)
speed("fastest")

pendown()

def isPrime(n):
    for i in range(2,int(n**0.5)+1):
        if n%i==0:
            return False
    return True

def makehex(sides):
    for making in range(0,sides):
        forward(hexsize)
        right(60)
    return    

fillcolor("red")

begin_fill()
makehex(6) # 1 is not prime
end_fill()
makehex(2) # 1 is not prime
left(120)
count = count + 1

fillcolor("blue")

def turn():
    global count
    if(isPrime(count)):
        begin_fill()
    makehex(6)
    end_fill()
    makehex(3)
    left(120)
    count = count + 1
    return

def straight(numhex):
    global count
    for length in range(0,numhex):
        if(isPrime(count)):
            begin_fill()
        makehex(6)
        end_fill()
        makehex(2)
        left(120)
        count = count + 1
    return

while(count < 9):
    if(isPrime(count)):
        begin_fill()
    makehex(6)
    end_fill()
    makehex(turnarray[count-2])
    left(120)
    count = count + 1

for layer in range(1,65):
    for sides in range(0,4):
        turn()
        straight(layer)
    turn()
    straight(layer+1)
    turn()
    straight(layer)


update()

Now I'm keen to "fold" the shapes and have a look at the 3D possibilities - anyone with me?



 

No comments:

Post a Comment