zak
08-06-2010, 07:03
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:
https://sites.google.com/site/zak31415/Home/primes_spiral.PNG
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
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.php?topic=3414.msg25380
when not pressing on the address bar i get the time 15.375 seconds
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
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:
https://sites.google.com/site/zak31415/Home/primes_spiral.PNG
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
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.php?topic=3414.msg25380
when not pressing on the address bar i get the time 15.375 seconds
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