Prime number distribution at sight

General FreeBASIC programming questions.
Post Reply
xlucas
Posts: 334
Joined: May 09, 2014 21:19
Location: Argentina

Prime number distribution at sight

Post by xlucas »

This is a well known trick discovered by somebody whose name I don't remember. Thing is, for the one who doesn't know, prime number distribution seems to be incredibly random and mathematicians have been for centuries trying to find an infallible rule, to no success. Yet, from time to time, some genius or some lucky guy with a lot of time, have found some hint of a rule and this is one of those lucky hints. I made my own code to create this well known effect. Take a look:

Code: Select all

Dim As Short x, y, thisl
Dim As Long n
Dim d As Byte, thistime As Byte = 0

ScreenRes 480, 480, 32

x = 239 : y = 239
d = 0 : n = 2
thisl = 1

Do
	Dim isdiv As Byte
	Dim curl As Short
	
	curl = thisl
	Do
		isdiv = 0
		For m As Short = 2 To Sqr(n)
			If (n Mod m) = 0 Then isdiv = -1 : Exit For
		Next m

		Dim c As Long
		If isdiv Then
			c = RGB(0, 0, 1)
		Else
			If (n Mod 4) = 3 Then
				c = RGB(0, 180, 80)
			Else
				c = RGB(200, 50, 0)
			End If
		End If
		PSet (x, y), c

		Select Case d
			Case 0 : y -= 1
			Case 1 : x -= 1
			Case 2 : y += 1
			Case Else : x += 1
		End Select
		
		curl -= 1 : n += 1
	Loop Until curl = 0
	
	If thistime Then
		thistime = 0
		thisl += 1
	Else
		thistime = 1
	End If
	
	d = (d + 1) Mod 4
Loop Until Abs(240 - x) > 240 And Abs(240 - y) > 240
GetKey
What the code does: Start at the middle of a piece of paper and write an integer. Say, one or two. Then go up one step and write the next. Continue in a square spiral around the numbers until you fill the paper and then mark which numbers are primes. Here, instead of writing the numbers, I've used pixels. If the number is a prime, I've coloured it. Otherwise, I've left it black (almost). I've added one more thing: when the number is indeed a primer, I check whether it's a complex (Gaussian) prime as well. If it is, I give it one colour and if it isn't, I give it another. Doesn't seem to change things much, for there it is. Anybody knows something else I could do to make it more interesting? Opinions?
jj2007
Posts: 2326
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

Re: Prime number distribution at sight

Post by jj2007 »

Looks like output from a not-so-brilliant PRNG ;-)
deltarho[1859]
Posts: 4313
Joined: Jan 02, 2017 0:34
Location: UK
Contact:

Re: Prime number distribution at sight

Post by deltarho[1859] »

This is random. BCryptGenRandom via CryptoRndII.
xlucas wrote:Anybody knows something else I could do to make it more interesting?
Play around with the colours - it is not sudden enough for my taste.

Image
xlucas
Posts: 334
Joined: May 09, 2014 21:19
Location: Argentina

Re: Prime number distribution at sight

Post by xlucas »

jj2007: Yeah, ha, ha... it looks like nature wasn't able to hide the non-randomness of the prime number distribution very well :P I actually created the program because I suspected that, if I separated Gaussian primes from non-Gaussian primes, I was going to see some conspicuous effect, but it wasn't so. It looks like both together look like one thing and each separate, also looks like that same thing. So, now that I had already made the program, I decided to post it here for others to examine.

deltarho: Yeah, I played with a few colour combinations. My idea was to make it possible for a person to distinguish Gaussian primes from non-Gaussian primes easily, but also relax and make as if all were the same and only distinguish them from non-primes, so the positive colours had to be different but equally bright, in contrast to the background colour, which should be dark (for non-primes). When I made them too bright, it was annoying to look at them, so I shortened the colour vectors a little bit. Of course, the idea here is not to create something random. Quite the contrary. The idea is to make it apparent that the prime number distribution is non-random! Sure, we all know it is not, because primes are defined by comprehension, but I mean, it's not only the rules that define them that they obey, but there are other rules that also dictate their distribution, as is vaguely noticeable in the output of this code.
dodicat
Posts: 7987
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Prime number distribution at sight

Post by dodicat »

Indeed there are rules to distribute prime numbers.
But we do not know these rules.
The number system is totally a human invention so must be prime numbers.
But Some biologists suggest that other species might use prime numbers.
Example:
https://www.bbc.co.uk/news/magazine-14305667

Personally I think this is just fanciful thinking.
But it all good because we can spend much time trying to find the prime distribution.
And perhaps become very famous if we succeed.
What are colour vectors?
grindstone
Posts: 862
Joined: May 05, 2015 5:35
Location: Germany

Re: Prime number distribution at sight

Post by grindstone »

Very interesting!
xlucas
Posts: 334
Joined: May 09, 2014 21:19
Location: Argentina

Re: Prime number distribution at sight

Post by xlucas »

dodicat wrote:Indeed there are rules to distribute prime numbers.
[...] Some biologists suggest that other species might use prime numbers.
Example:
https://www.bbc.co.uk/news/magazine-14305667
[...]
Indeed, I had read about this thing on cicadas before! It makes sense, in my opinion, that animals and even simpler organisms, such as viruses, "use" prime numbers. It's not that they know what they're doing, but from trial and error, nature repeatedly finds picking prime numbers convenient for some tasks, because the existance of sub-cycles in some processes may be "undesirable", that is, produce negative effects. Just like some times, it's better for something to be even than odd (say, the number of legs in an animal), sometimes it's better for a number to be prime than non-prime and this goes beyond our understanding of the fact.

A deeper thought on that:
There is a discussion on whether the human mind created mathematics or mathematics have to be the way they are so that the human mind is the way it is. I believe this is a specific case of the debate on whether the so called "anthropic principle" is a valid argument in support for some ideas. In general, I'd say it is, but one must understand that, a double-implication ("if and only if") in logics is much stronger than a simple implication, because the order of causality (i.e.: the direction of the arrow of time) becomes irrelevant. When our own nature becomes part of a double implication, it makes more sense to understand not that we cause the other part or that we are caused by it, but rather, that both us and that other part form a single thing. In programming, a double implication is typically understood as equivalence. So, how about "we are mathematics"?
dodicat wrote:What are colour vectors?
I mean, a colour, when you divide it in red, green and blue becomes a triplet. Colour as perceived by human eyes is tridimensional, so to define a specific colour, we need three coordinates and what we have is a 3D vector. Yep, maybe I like math too much XD. But what I wanted to say is I shortened the vectors, like, I made their length shorter, that is, I made them more similar by making them darker, because if they were more different between themselves, they also were too bright, so even though I needed different colours for contrast, I didn't want so much contrast with the background.
dodicat
Posts: 7987
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Prime number distribution at sight

Post by dodicat »

Thanks xlucas.
I don't think that numbers (mathematics) can live long after the human race is gone.
It will be a memory which is not possible to be remembered.
Is this not the answer, so all further philosophy is purely for fun.

The pixel colours all lie within a sphere of radius 255

I remember doing the vector thing.

Code: Select all

 

'8 bit to 32 bit
Sub set(clr() as ulong)
   ReDim As ULong tmp(101*101)
for i as integer = 0 to 255
   ScreenRes 100,100,8,,-1
   Dim As Any Ptr im=ImageCreate(100,100,,8)
   Paint im,(5,5),i
   BSave("tmp.bmp",im)
   ScreenRes 100,100,32,,-1
   BLoad("tmp.bmp",@tmp(0))
    clr(i)=tmp(50)
   ImageDestroy im:im=0
Next i
Kill "tmp.bmp"
End Sub


dim as ulong clr(255)

set(clr()) 'set colours 0 to 255 8 bit into 32 bit.

type pt  'simulate 3D space
as single x,y,z
end type

dim shared as pt a(255)
'set colours 0 to 255 as rgb (vectors)
for n as long=0 to 255
    '                  red                            green                       blue
        a(n)=type(Cptr(Ubyte Ptr,@clr(n))[2],Cptr(Ubyte Ptr,@clr(n))[1],Cptr(Ubyte Ptr,@clr(n))[0])
next
    
function closest(c() as ulong,v as ulong) as ulong
    dim as ulong res
     #define dist(p1,p2) (p1.x-p2.x)*(p1.x-p2.x) + (p1.y-p2.y)*(p1.y-p2.y) + (p1.z-p2.z)*(p1.z-p2.z)
     'get r,g,b of v
    dim as pt pv=type(Cptr(Ubyte Ptr,@v)[2],Cptr(Ubyte Ptr,@v)[1],Cptr(Ubyte Ptr,@v)[0])
    dim as single dt=1e20
    for n as long=0 to 255
        var distance=dist(a(n),pv)
        if dt> distance then dt = distance:res=c(n) 'catch the smallest
    next n
    return res
end function

screen 19,32
'create an image with circles
dim as ulong col
dim as any ptr i=imagecreate(300,600,0)
for n as long=0 to 200
    circle i,(rnd*300,rnd*600),2+rnd*20,rgb(rnd*255,rnd*255,rnd*255),,,,f
next

put(0,0),i,pset
draw string(10,2),"rgb colours"

'second image to hold the 256 colours
dim as any ptr im2=imagecreate(300,600,0)

for x as long=0 to 299
    for y as long=0 to 599
        col=closest(clr(),point(x,y,i))
        pset im2,(x,y),col
    next
next

put(400,0),im2,pset
draw string(400,2),"256 colours"
sleep
imagedestroy i
imagedestroy im2

 
dafhi
Posts: 1650
Joined: Jun 04, 2005 9:51

Re: Prime number distribution at sight

Post by dafhi »

i embrace the idea of a dual nature of conceptual entities. something "to be discovered" sums it up.

plus, i don't think humans are the only intelligent things
shoot there's even debate about certain recently observed traits in birds and at least one of the primates (ape, chimp, i forget)
Provoni
Posts: 514
Joined: Jan 05, 2014 12:33
Location: Belgium

Re: Prime number distribution at sight

Post by Provoni »

Prime numbers are emergent from a rule imposed on the natural numbers.

I say that prime numbers come in different orders such that the lower orders are prime to the higher orders:

Order 0 prime: 1 (is prime to order 1)
Order 1 primes: 2, 3, 5, 7, 11, etc... (are prime to order 2)
Order 2 primes: 4, 6, 9, 10, 14, 15, etc... (are prime to order 3)
Etc...

If looking at it as a rule then start with the number 2 and all multiples of 2 cannot be prime. So the next unmarked number is 3 and now also all multiples of 3 cannot be prime. Then 5 comes up and now also, etc... This is a rule of exclusion and one that grows more complex as it goes on since it builds upon itself.

Nature (evolution) has learned to use this rule of exclusion to avoid certain species that have periodic cycles. Thus prime numbers in essence can be seen as anti-periodic and that is what you can expect from them.
dodicat
Posts: 7987
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Prime number distribution at sight

Post by dodicat »

Just made up another prime number generator.
Principle of n th. term of an arithmetic series (a+(n-1)*d)
create all the arithmetic seqeuencies
4+(n-1)*2
then
6+(n-1)*3
then
8+(n-1)*4
...

Anything not included in these sequences is prime
Proof ; er ... primes are not in any arithmetic series, all other numbers are .. er ...
Checked against the standard eratosthenes sieve

Code: Select all

 
Screen 20
Dim Shared As Long count  'common to check against standard sieve only
Print "my method"


Sub getprimes(lim As Long)
    Dim As Long num,z
    Redim As Byte a(1 To (lim-3)*2)
    For k As Long=2 To lim
        For n As Long=k To lim-k 'shrink to suit
            If k=2 And n=lim-k Then num=z
            z= k+(n-1)*k       'k term of arithmetis series
            If num And z>num Then  Exit For
            a(z)=1
        Next
    Next k
    Print
    For n As Long=Lbound(a)+1 To Ubound(a)
        If a(n)=0 Then count=n: Print n;
    Next
    Print
End Sub
sleep 50
Dim As Double t=Timer
getprimes(2000)  ''< -------------  number of primes
Print Timer-t
'shared count passes to line 52
Print
Print


'eratosthenes sieve 
Sub generateprimes(primes() As Integer,nmax As Integer)
    Redim primes(1 To nmax)
    Var np = 0
    Dim As Integer i, k
    For k = 2 To nmax
        If primes(k) = 0 Then
            np += 1
            primes(np) = k
            For i = 2*k To nmax Step k
                primes(i) = 1
            Next i
        End If
    Next k
    Redim Preserve primes(1 To np)
End Sub

t=Timer
Redim As Integer p()
generateprimes(p(),count)
Print "standard sieve"
For n As Integer=Lbound(p) To Ubound(p)
    Print p(n);
Next
Print
Print Timer-t
Sleep  
Provoni
Posts: 514
Joined: Jan 05, 2014 12:33
Location: Belgium

Re: Prime number distribution at sight

Post by Provoni »

A Natural Prime-Generating Recurrence: https://cs.uwaterloo.ca/journals/JIS/VO ... land21.pdf

Code: Select all

function gcd(byval a as longint,byval b as longint)as longint
  	do while b<>0
      var t=b
      b=a mod b
      a=t
  	loop
  	return a
end function

screenres 640,480,32
dim as longint p1,p2,i,j

p1=7
for i=2 to 10000000
	p2=p1+gcd(i,p1)
	j=p2-p1
	swap p1,p2
	if j>1 then print str(j)+" ";
next i

beep
sleep
Post Reply