# PureBasic Forum

 It is currently Wed May 22, 2013 8:01 am

 All times are UTC + 1 hour

 Page 1 of 1 [ 8 posts ]
 Print view Previous topic | Next topic
Author Message
 Post subject: Prime numbers spiral explorationPosted: Thu May 26, 2011 1:52 pm

Joined: Sat Feb 19, 2005 2:46 pm
Posts: 1334
Location: Pas-de-Calais, France
Some guys played a bit with prime numbers on french forum. I've imagined a way to find some special patterns if numbers were distributed on 2D ; I've thought of spiral and discovered someone did that before (isn't it strange ) : http://www.numberspiral.com/index.html. Anyway, I tried to alter dynamically the spiral parameters to push further the exploration. Here's the code, if you find the magical pattern, don't forget me

Code:
;**********************************
;
; Primes numbers spiral exploration
; 26/05/2011
; by djes (djes@free.fr)
; Thx to Sirius-2337, Demivec, Gnasen for IsPrime optim
; Thx to DjPoke, Fig, zaphod, graph100, LSI & Nat the Great for LookForPrimes() optim
;
;**********************************

Global Dim Primes(80000)

Procedure IsPrime(number.l)

If number = 2
ProcedureReturn 1
ElseIf number % 2 = 0
ProcedureReturn 0
Else
x = Sqr(number)
For t = 3 To x Step 2
If number % t = 0
ProcedureReturn 0
EndIf
Next t
EndIf

ProcedureReturn 1

EndProcedure

Procedure SpiralDraw()

cx = ww / 2 - 20 : cy = wh / 2

Angle.d = #PI / (GetGadgetState(0) / 100)
Deviation.d = #PI / (GetGadgetState(1) / 10 )

i.d = 0
e.d = 0
;PreviousPrime = 0
StartDrawing(ImageOutput(0))
Box(0, 0, ww - 40, wh, \$FFFFFF)
If TextFlag
DrawingMode(#PB_2DDrawing_Transparent|#PB_2DDrawing_XOr)
For u = 0 To PrimesNb - 1
x = cx + e * Sin(i)
y = cy + e * Cos(i)
DrawText(x, y, Str(Primes(u)), RGB(\$FF, \$FF, 0))
i = Angle * Primes(u)
;e = Deviation * Primes(u) ;deviation from the center is based on the prime value
e + Deviation              ;deviation is linear
Next u
EndIf

i.d = 0
e.d = 0
DrawingMode(#PB_2DDrawing_Default)
For u = 0 To PrimesNb - 1
x = cx + e * Sin(i)
y = cy + e * Cos(i)
If x >= 0 And x < ww - 40 And y >= 0 And y < wh
Plot(x, y, 0)
EndIf
i = Angle * Primes(u)
;e = Deviation * Primes(u) ;deviation from the center is based on the prime value
e + Deviation              ;deviation is linear
;PreviousPrime = Primes(u)
Next u

StopDrawing()
StartDrawing(WindowOutput(0))
DrawImage(ImageID(0), 0, 0)
DrawText(0,  0, "Angle : PI/" + StrD(GetGadgetState(0) / 100, 2), 0, \$FFFFFF)
DrawText(0, 20, "Deviation : PI/" + Str(GetGadgetState(1)), 0, \$FFFFFF)
StopDrawing()

EndProcedure

Procedure GUIInit()

ww = WindowWidth(0) : wh = WindowHeight(0)
CreateImage(0, ww - 40, wh)
TrackBarGadget(0, WindowWidth(0) - 40, 16, 20, WindowHeight(0) - 16, 1, 31410, #PB_TrackBar_Vertical)
TrackBarGadget(1, WindowWidth(0) - 20, 16, 20, WindowHeight(0) - 16, 1, 4000, #PB_TrackBar_Vertical)
CheckBoxGadget(2, WindowWidth(0) - 40,  1, 40, 16, "Text")

EndProcedure

Procedure LookForPrimes(RangeSearch)

;   TextGadget(4, 5, wh / 2 - 20, ww - 40 - 5, 20, "Looking for primes numbers - Please wait", #PB_Text_Right)
;   ProgressBarGadget(3,  5, wh / 2, ww - 40 - 5, 20, 0, RangeSearch, #PB_ProgressBar_Smooth)

;   PrimesNb = 0
;   For u = 2 To RangeSearch
;     If IsPrime(u)
;       Primes(PrimesNb) = u
;       PrimesNb + 1
;     EndIf
;   Next u
;   SetGadgetText(4, Str(PrimesNb) + " found")
;   Delay(1000)

; algo nat the great pour blitzmax
; port en pb by zaphod - 23/05/2011

Define num.i = RangeSearch
Define sqrnum.i = Sqr(RangeSearch)+1
Dim PrimeFlags.i(RangeSearch)
ReDim Primes.i(RangeSearch)
Define tim.i

PrimesNb = 1

For x = 3 To sqrnum Step 2
If PrimeFlags(x) = #False
Primes(PrimesNb) = x
PrimesNb+1

tim = x + x
Repeat
PrimeFlags(tim) = #True
tim + x
Until tim >= num
EndIf
Next

Primes(0) = 2
For y = x To num Step 2
If PrimeFlags(y) = #False
Primes(PrimesNb) = y
PrimesNb+1
EndIf
Next

EndProcedure

;-MAIN

WindowBounds(0, 500, 500, 4000, 4000)

TextFlag = #False

GUIInit()
LookForPrimes(500000)
SpiralDraw()

Repeat

Event = WaitWindowEvent()

Select Event

Case #PB_Event_SizeWindow

GUIInit()
SpiralDraw()

Case 0, 1

SpiralDraw()

Case 2

TextFlag = ~TextFlag
SpiralDraw()

EndSelect

Case #PB_Event_CloseWindow
Quit = 1

EndSelect

Until Quit = 1

EndIf

End

Edit: optimised IsPrime() function

_________________
The Shooting Crew ~> http://www.shootingcrew.com/
Bobble Puzzle, Purebreaker 3 ~> http://djes.free.fr

Last edited by djes on Thu May 26, 2011 8:23 pm, edited 5 times in total.

Top

 Post subject: Re: Prime numbers spiral explorationPosted: Thu May 26, 2011 3:21 pm
 User

Joined: Sat May 14, 2011 10:39 am
Posts: 37
Nice Code, but it work realy faster if you change your IsPrime-Function a bit

Code:
Procedure IsPrime(Number.l)

For t = 2 To Number - 1
If Number % t = 0
ProcedureReturn 0
EndIf
Next t

ProcedureReturn 1

EndProcedure

You can Return 0 as soon as Number % t = 0 and don't have to check all the other numbers.

Top

 Post subject: Re: Prime numbers spiral explorationPosted: Thu May 26, 2011 3:34 pm

Joined: Mon Jul 25, 2005 3:51 pm
Posts: 2399
Location: Utah, USA
Here's a one up on Sirus-2337's improvement:
Code:
Procedure IsPrime(number.l)

x = Sqr(number)
For t = 2 To x
If number % t = 0
ProcedureReturn 0
EndIf
Next t

ProcedureReturn 1

EndProcedure

Nice code. I'll definitely be taking a deeper look into the spiral patterns.

_________________

Top

 Post subject: Re: Prime numbers spiral explorationPosted: Thu May 26, 2011 3:36 pm

Joined: Sat Feb 19, 2005 2:46 pm
Posts: 1334
Location: Pas-de-Calais, France
Thanks guys

_________________
The Shooting Crew ~> http://www.shootingcrew.com/
Bobble Puzzle, Purebreaker 3 ~> http://djes.free.fr

Top

 Post subject: Re: Prime numbers spiral explorationPosted: Thu May 26, 2011 4:14 pm

Joined: Wed Aug 24, 2005 8:39 am
Posts: 2558
Location: Southwest OH - USA
I keep getting blown away by things I'll likely never have a use for.

Very impressive.

Thanks for sharing.

Top

 Post subject: Re: Prime numbers spiral explorationPosted: Thu May 26, 2011 4:39 pm
 Enthusiast

Joined: Wed Sep 24, 2008 12:21 am
Posts: 255
the end is not reached yet (and there may be more to tune )
Code:
Procedure IsPrime(number.l)

If number = 2
ProcedureReturn 1
ElseIf number % 2 = 0
ProcedureReturn 0
Else
x = Sqr(number)
For t = 3 To x Step 2
If number % t = 0
ProcedureReturn 0
EndIf
Next t
EndIf

ProcedureReturn 1

EndProcedure

However, Im still very impressed by the pure beauty of this. I never thought about prime numbers in this way and I would be interested if there is some research in this direction already done. Im sure there must be much material about this fact, because there seems to be no other themes next to prime numbers for theoretical mathematics

_________________
pb 5.11

Top

 Post subject: Re: Prime numbers spiral explorationPosted: Thu May 26, 2011 8:24 pm

Joined: Sat Feb 19, 2005 2:46 pm
Posts: 1334
Location: Pas-de-Calais, France
Optimised LookForPrimes() by Nat the Great, DjPoke, Fig, zaphod, graph100 & LSI

_________________
The Shooting Crew ~> http://www.shootingcrew.com/
Bobble Puzzle, Purebreaker 3 ~> http://djes.free.fr

Top

 Post subject: Re: Prime numbers spiral explorationPosted: Fri May 27, 2011 10:44 pm

Joined: Sat Feb 19, 2005 2:46 pm
Posts: 1334
Location: Pas-de-Calais, France
Color (difference between two numbers) ; keyboard shortcuts : up/down, left/right, +/-
Code:
;**********************************
;
; Primes numbers spiral exploration
; 26/05/2011
; by djes (djes@free.fr)
; Thx to Sirius-2337, Demivec, Gnasen for IsPrime optim
; Thx to DjPoke, Fig, zaphod, graph100, LSI & Nat the Great for LookForPrimes() optim
;
;**********************************

Global Dim Primes(80000)

Procedure IsPrime(number.l)

If number = 2
ProcedureReturn 1
ElseIf number % 2 = 0
ProcedureReturn 0
Else
x = Sqr(number)
For t = 3 To x Step 2
If number % t = 0
ProcedureReturn 0
EndIf
Next t
EndIf

ProcedureReturn 1

EndProcedure

Procedure SpiralDraw()

cx = ww / 2 - 20 : cy = wh / 2

Angle.d = #PI / (GetGadgetState(0) / 10000)
Deviation.d = #PI / (GetGadgetState(1) / 10 )

i.d = 0
e.d = 0
PreviousPrime = 0
StartDrawing(ImageOutput(0))
Box(0, 0, ww - 40, wh, 0)
If TextFlag
DrawingMode(#PB_2DDrawing_Transparent|#PB_2DDrawing_XOr)
For u = 0 To PrimesNb - 1
x = cx + e * Sin(i)
y = cy + e * Cos(i)
If x >= 0 And x < ww - 40 And y >= 0 And y < wh
DrawText(x, y, Str(Primes(u)), RGB(\$FF, \$FF, 0))
EndIf
i = Angle * Primes(u)
;e = Deviation * Primes(u) ;deviation from the center is based on the prime value
e + Deviation              ;deviation is linear
Next u
EndIf

i.d = 0
e.d = 0
DrawingMode(#PB_2DDrawing_Default)
For u = 0 To PrimesNb - 1
x = cx + e * Sin(i)
y = cy + e * Cos(i)
If x >= 0 And x < ww - 40 And y >= 0 And y < wh
Plot(x, y, (Primes(u) - PreviousPrime) * ColorFactor)
EndIf
i = Angle * Primes(u)
;e = Deviation * Primes(u) ;deviation from the center is based on the prime value
e + Deviation              ;deviation is linear
PreviousPrime = Primes(u)
Next u

StopDrawing()
StartDrawing(WindowOutput(0))
DrawImage(ImageID(0), 0, 0)
DrawText(0,  0, "Angle : PI/" + StrD(GetGadgetState(0) / 10000, 4), \$FFFFFF, 0)
DrawText(0, 16, "Deviation : PI/" + Str(GetGadgetState(1)), \$FFFFFF, 0)
DrawText(0, 32, "Color Factor : " + Str(ColorFactor), \$FFFFFF, 0)
StopDrawing()

EndProcedure

Procedure GUIInit()

ww = WindowWidth(0) : wh = WindowHeight(0)
CreateImage(0, ww - 40, wh)
TrackBarGadget(0, WindowWidth(0) - 40, 16, 20, WindowHeight(0) - 16, 1, 3141600, #PB_TrackBar_Vertical)
TrackBarGadget(1, WindowWidth(0) - 20, 16, 20, WindowHeight(0) - 16, 1, 16000, #PB_TrackBar_Vertical)
CheckBoxGadget(2, WindowWidth(0) - 40,  1, 40, 16, "Text")

EndProcedure

Procedure LookForPrimes(RangeSearch)

;   TextGadget(4, 5, wh / 2 - 20, ww - 40 - 5, 20, "Looking for primes numbers - Please wait", #PB_Text_Right)
;   ProgressBarGadget(3,  5, wh / 2, ww - 40 - 5, 20, 0, RangeSearch, #PB_ProgressBar_Smooth)

;   PrimesNb = 0
;   For u = 2 To RangeSearch
;     If IsPrime(u)
;       Primes(PrimesNb) = u
;       PrimesNb + 1
;     EndIf
;   Next u
;   SetGadgetText(4, Str(PrimesNb) + " found")
;   Delay(1000)

; algo nat the great pour blitzmax
; port en pb by zaphod - 23/05/2011

Define num.i = RangeSearch
Define sqrnum.i = Sqr(RangeSearch)+1
Dim PrimeFlags.i(RangeSearch)
ReDim Primes.i(RangeSearch)
Define tim.i

PrimesNb = 1

For x = 3 To sqrnum Step 2
If PrimeFlags(x) = #False
Primes(PrimesNb) = x
PrimesNb+1

tim = x + x
Repeat
PrimeFlags(tim) = #True
tim + x
Until tim >= num
EndIf
Next

Primes(0) = 2
For y = x To num Step 2
If PrimeFlags(y) = #False
Primes(PrimesNb) = y
PrimesNb+1
EndIf
Next

EndProcedure

;-MAIN

WindowBounds(0, 500, 500, 4000, 4000)

TextFlag = #False
ColorFactor = 16

GUIInit()
LookForPrimes(2000000)
SpiralDraw()

Repeat

Event = WaitWindowEvent()

Select Event

Case #PB_Event_SizeWindow

GUIInit()
SpiralDraw()

Case 0, 1

SpiralDraw()

Case 2

TextFlag = ~TextFlag
SpiralDraw()

EndSelect

Case 1

SpiralDraw()

Case 2

SpiralDraw()

Case 3

ColorFactor + 1
SpiralDraw()

Case 4

ColorFactor - 1
SpiralDraw()

Case 5

SpiralDraw()

Case 6

SpiralDraw()

EndSelect

Case #PB_Event_CloseWindow
Quit = 1

EndSelect

Until Quit = 1

EndIf

End

_________________
The Shooting Crew ~> http://www.shootingcrew.com/
Bobble Puzzle, Purebreaker 3 ~> http://djes.free.fr

Top

 Display posts from previous: All posts1 day7 days2 weeks1 month3 months6 months1 year Sort by AuthorPost timeSubject AscendingDescending
 Page 1 of 1 [ 8 posts ]

 All times are UTC + 1 hour

#### Who is online

Users browsing this forum: No registered users and 3 guests

 You cannot post new topics in this forumYou cannot reply to topics in this forumYou cannot edit your posts in this forumYou cannot delete your posts in this forum

Search for:
 Jump to:  Select a forum ------------------ PureBasic    Coding Questions    Game Programming    3D Programming    Assembly Programming    The PureBasic Editor    The PureBasic Form Designer    General Discussion    Feature Requests and Wishlists    Tricks 'n' Tips Bug Reports    Bugs - Windows    Bugs - Linux    Bugs - Mac OSX    Bugs - Documentation OS Specific    AmigaOS    Linux    Windows    Mac OSX Miscellaneous    Announcement    Off Topic Showcase    Applications - Feedback and Discussion    PureFORM & JaPBe    TailBite