Results 1 to 5 of 5

Thread: prime numbers spiral

  1. #1

    prime numbers spiral

    Hi
    the following program is an implementation of the prime numbers spiral explained here:
    http://en.wikipedia.org/wiki/Ulam_spiral
    there are some patterns which are just a plotting of some equations which produce prime numbers in a small scale such as euler equation:
    P(n) = n2 − n + 41 , it is failed when the number is 41
    the problem of the primality or not of number one discussed here:
    http://wiki.answers.com/Q/Why_is_1_not_a_prime_number
    http://wiki.answers.com/Q/Is_one_a_prime_number
    http://mathworld.wolfram.com/PrimeNumber.html
    the program here is just a translation of the pencil and paper way in plotting the spiral, there are may be a more ways in doing that
    the first program will display the spiral for a few numbers with a big dots, the purpose is to be sure the program is working correctly, the new thinbasic IsPrime function is handy in detecting the primes:


    [code=thinbasic]Uses "UI"

    Dim hWin As DWord = Canvas_Window("primes spiral", 1, 1, 800, 600 )
    Canvas_Attach(hWin, 0, %TRUE) ' <- double buffer
    Dim t1, t2 As Quad
    hiresTimer_Init
    t1 = hiresTimer_Get
    Canvas_Clear(%BLACK)
    Dim i,j,colrR,colrG,colrB As Long
    Dim x, y, dots As Long
    x=400:y=300
    dim turn,num,points_per_edge,edgecycle as long
    turn=0:num=1
    points_per_edge = 1: edgecycle = 0
    'ploting point number 1 :
    Canvas_Ellipse(x-4, y-4, x+4, y+4, Rgb(255, 255, 0),Rgb(255, 255, 0))
    Canvas_Redraw

    For i = 1 To 9

    Plotting()

    next

    t2 = HiResTimer_Get

    MsgBox hWin, "Time taken:"+Format$((t2-t1)/1000000, "#.000")+" second --" + dots + " dots drawn", %MB_APPLMODAL


    Canvas_Window end


    SUB Plotting()

    turn = turn + 1
    If turn = 5 Then turn = 1
    edgecycle = edgecycle + 1

    If edgecycle = 3 Then
    edgecycle = 1
    points_per_edge = points_per_edge + 1
    end if

    For j = 1 To points_per_edge

    num = num + 1

    If IsPrime(num)=%TRUE Then
    colrR = 0 : colrG = 255 : colrB = 0
    else
    colrR = 255 : colrG = 0 : colrB = 0
    end if

    Select Case turn
    Case 1
    x = x + 24
    Case 2
    y = y - 24
    Case 3
    x = x - 24
    Case 4
    y = y + 24
    End Select

    Canvas_Ellipse(x-4, y-4, x+4, y+4, Rgb(colrR, colrG, colrB),Rgb(colrR, colrG, colrB))
    dots = dots + 1
    Canvas_Redraw
    Next j


    END SUB

    [/code]
    the second program will plot the 250500 numbers, the primes in white, the composites in black, if you want to speed the plotting just press the left mouse button over the title bar as reported here:
    http://community.thinbasic.com/index...=3414.msg25380
    when not pressing on the address bar i get the time 15.375 seconds
    [code=thinbasic]Uses "UI"

    Dim hWin As DWord = Canvas_Window("primes spiral", 1, 1, 800, 600 )
    Canvas_Attach(hWin, 0, %TRUE) ' <- double buffer
    Dim t1, t2 As Quad
    hiresTimer_Init
    t1 = hiresTimer_Get
    Canvas_Clear(%BLACK)
    Dim i,j,colrR,colrG,colrB As Long
    Dim x, y, dots As Long
    Dim ScreenWidth As Long = 800: Dim ScreenHeight As Long = 600
    x = ScreenWidth/2 :y = ScreenHeight/2
    Dim turn,num,points_per_edge,edgecycle As Long
    turn=0:num=1
    points_per_edge = 1: edgecycle = 0

    For i = 1 To 1000
    Plotting()
    Next
    'ploting point number 1 (thick dot):
    Canvas_Ellipse(ScreenWidth/2-2, ScreenHeight/2-2, ScreenWidth/2+2, ScreenHeight/2+2, Rgb(255,255, 0),Rgb(255, 255, 0))
    Canvas_Redraw
    t2 = HiResTimer_Get
    MsgBox hWin, "Time taken:"+Format$((t2-t1)/1000000, "#.000")+" second --" + dots + " dots drawn", %MB_APPLMODAL
    Canvas_Window End

    SUB Plotting()

    turn = turn + 1
    If turn = 5 Then turn = 1
    edgecycle = edgecycle + 1

    If edgecycle = 3 Then
    edgecycle = 1
    points_per_edge = points_per_edge + 1
    end if

    For j = 1 To points_per_edge
    num = num + 1
    If IsPrime(num)=%TRUE Then
    colrR = 255: colrG = 255 : colrB = 255
    else
    colrR = 0 : colrG = 0 : colrB = 0
    end if

    Select Case turn
    Case 1
    x = x + 1
    Case 2
    y = y - 1
    Case 3
    x = x - 1
    Case 4
    y = y + 1
    End Select

    Canvas_Color( Rgb(colrR, colrG, colrB))
    Canvas_SetPixel(x, y)
    dots = dots + 1
    'Canvas_Redraw
    Next j
    Canvas_Redraw

    END SUB

    [/code]
    Attached Thumbnails Attached Thumbnails Click image for larger version. 

Name:	primes spiral1.PNG 
Views:	212 
Size:	49.1 KB 
ID:	6673  

  2. #2
    thinBasic author ErosOlmi's Avatar
    Join Date
    Sep 2004
    Location
    Milan - Italy
    Age
    51
    Posts
    8,343
    Blog Entries
    2
    Rep Power
    10

    Re: prime numbers spiral

    Thank you zak for this interesting script
    Math visualization is always very fascinating

    Can I put those scripts in next thinBasic release examples giving credit to you?
    www.thinbasic.com | www.thinbasic.com/community/ | psch.thinbasic.com
    Win10Pro 64bit - 8GB Ram - Intel i7 M620 2.67GHz - NVIDIA Quadro FX1800M 1GB

  3. #3

    Re: prime numbers spiral

    Hi Eros
    yes of course, also you can refine it as needed ,thanks.
    i have faced a strange phenomena, when running firefox on the thinbasic forum and choosing "work offline" from file menu the speed of the plot is as you click on the address bar. but when it in google no effect. can you confirm this, this is strange.

  4. #4

    Re: prime numbers spiral

    i have reproduced the effect many times, more than 10 times, but not now, it seems the effect appears sometimes, . i can't reproduce it again.

  5. #5
    Super Moderator Petr Schreiber's Avatar
    Join Date
    Aug 2005
    Location
    Brno - Czech Republic
    Posts
    6,839
    Blog Entries
    3
    Rep Power
    697

    Re: prime numbers spiral

    Hi Zak,

    very nice example, thanks a lot! I get similar time here.
    You can get to ~3 seconds by redrawing just once:
    [code=thinbasic]
    Uses "UI"

    Dim hWin As DWord = Canvas_Window("primes spiral", 1, 1, 800, 600 )
    Canvas_Attach(hWin, 0, %TRUE) ' <- double buffer
    Dim t1, t2 As Quad
    HiResTimer_Init
    t1 = HiResTimer_Get
    Canvas_Clear(%BLACK)
    Dim i,j,colrR,colrG,colrB As Long
    Dim x, y, dots As Long
    Dim ScreenWidth As Long = 800: Dim ScreenHeight As Long = 600
    x = ScreenWidth/2 :y = ScreenHeight/2
    Dim turn,num,points_per_edge,edgecycle As Long
    turn=0:num=1
    points_per_edge = 1: edgecycle = 0

    For i = 1 To 1000
    Plotting()
    Next
    Canvas_Redraw

    'ploting point number 1 (thick dot):
    Canvas_Ellipse(ScreenWidth/2-2, ScreenHeight/2-2, ScreenWidth/2+2, ScreenHeight/2+2, Rgb(255,255, 0),Rgb(255, 255, 0))
    Canvas_Redraw
    t2 = HiResTimer_Get
    MsgBox hWin, "Time taken:"+Format$((t2-t1)/1000000, "#.000")+" second --" + dots + " dots drawn", %MB_APPLMODAL
    Canvas_Window End

    Sub Plotting()

    turn = turn + 1
    If turn = 5 Then turn = 1
    edgecycle = edgecycle + 1

    If edgecycle = 3 Then
    edgecycle = 1
    points_per_edge = points_per_edge + 1
    End If

    For j = 1 To points_per_edge
    num = num + 1
    If IsPrime(num)=%TRUE Then
    colrR = 255: colrG = 255 : colrB = 255
    Else
    colrR = 0 : colrG = 0 : colrB = 0
    End If

    Select Case turn
    Case 1
    x = x + 1
    Case 2
    y = y - 1
    Case 3
    x = x - 1
    Case 4
    y = y + 1
    End Select

    Canvas_Color( Rgb(colrR, colrG, colrB))
    Canvas_SetPixel(x, y)
    dots = dots + 1
    'Canvas_Redraw
    Next j


    End Sub
    [/code]

    Maybe writing directly to bitmap (Canvas_BitmapGet, Canvas_BitmapSet) with array overlay could bring some speedup.

    Regarding the FireFox interference - I can observe it for OpenGL programs as well, the frame rate can get quite jumpy with FF open.


    Petr
    Learn 3D graphics with ThinBASIC, learn TBGL!
    Windows 7 64bit - Intel Core 2 Duo T6600 @ 2.2GHz - 4 GB RAM - NVIDIA GeForce G210M 512MB
    Windows 8 64bit - Intel Core i5-3350P @ 3.1GHz - 8 GB RAM - NVIDIA GeForce GT640 3GB

Similar Threads

  1. prime numbers (1-1000)
    By Lionheart008 in forum Sources, Templates, Code Snippets, Tips and Tricks, Do you know ...
    Replies: 4
    Last Post: 05-07-2010, 17:31

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •