PDA

View Full Version : prime numbers (1-1000)



Lionheart008
05-07-2010, 16:38
showing simple prime numbers to 1000 :) I've have translated this example from powerbasic (credit to author: borje hagsten) side (very old one and adapted for thinbasic).


' Empty GUI script created on 07-05-2010 16:23:08 by frank (ThinAIR)

Uses "ui"

Begin Const, %WM_USER
%listy
%labely
%texty
End Const

Declare Function GetDlgItem Lib "USER32.DLL" Alias "GetDlgItem" (ByVal hDlg As Long, ByVal nIDDlgItem As Long) As Long

Function TBMain() As Long
Local hDlg As Long

Dialog New 0, "thinbasic: get prime numbers", -1, -1, 180, 160, %WS_SYSMENU To hDlg

Control Add LISTBOX, hDlg, %listy, , 4, 20, 100, 120, _
%WS_TABSTOP Or %WS_VSCROLL, %WS_EX_CLIENTEDGE

Control Add Button, hDlg, %IDOK, "Get", 112, 4, 50, 14,
Control Add Button, hDlg, %IDCANCEL, "Exit", 112, 20, 50, 14,

Control Add Label, hDlg, %labely, "Up to:", 4, 4, 40, 10,
Control Add Textbox, hDlg, %texty, "1000", 44, 4, 60, 12, %ES_NUMBER, %WS_EX_CLIENTEDGE

Dialog Show Modal hDlg Call DlgCallback
End Function

CallBack Function DlgCallback()
Select Case CBMSG

Case %WM_INITDIALOG
Control Set Focus CBHNDL, %IDOK

Case %WM_COMMAND
Select Case CBCTL

Case %IDOK
Local Buf As String
Local Num As Long
Local t As Single

Control Get Text CBHNDL, %texty To Buf
Num = Val(buf)

If Num Then
LISTBOX Reset CBHNDL, %listy
t = Timer
GetPrimes GetDlgItem(CBHNDL, %listy), Num
t = Timer - t

Control Send CBHNDL, %listy, %LB_GETCOUNT, 0, 0 To Num
MsgBox 0, Format$(Num) & " prime numbers listed in " & Format$(t, "0.000") & " seconds."
End If

Case %IDCANCEL
Dialog End CBHNDL

End Select
End Select
End Function

Sub GetPrimes (ByVal Lst As Long, ByVal MaxNum As Long)
Local Buf As String
Local i As Long
Local Num As Long
Local Flag As Byte

Flag = 1
For Num = 1 To MaxNum
For i = 2 To Sqr(Num)
If num And Mod(num,i) = 0 Then
Flag = 0
Exit For
End If
Next I

If Flag Then
Buf = Str$(Num)
SendMessage Lst, %LB_ADDSTRING, 0, StrPtr(Buf)
Else
Flag = 1
End If
Next Num

End Sub

edit: have modified this example and deleted two (not useful) lines.

best regards, frank

Michael Hartlef
05-07-2010, 16:53
Nice example. :eusaclap: Thanks for sharing!

Petr Schreiber
05-07-2010, 16:55
Hi Frank,

thanks!
Little tip for you - instead of:


Begin Const, %WM_USER
%listy
%labely
%texty
End Const


you can use more self documenting:


Begin ControlID
%listy
%labely
%texty
End ControlID


As you say, this is based on PowerBasic example. Maybe it would be polite to cite the original author.


Thanks!,
Petr

Lionheart008
05-07-2010, 17:02
you are welcome mike, petr :)

1)
As you say, this is based on PowerBasic example. Maybe it would be polite to cite the original author.

see my first post ;)

2)
Begin ControlID
%listy
%labely
%texty
End ControlID

I have had a look at the manual. And it's some kind of a new thing to use more "Begin ControlID" instead of "begin const" ("begin const, %WM_User") I have picked this one from manual ;)

servus, frank

Petr Schreiber
05-07-2010, 17:31
Hi Frank,

thanks for updating it!
Yes, Begin/End ControlID is quite new, that is why brought it up. It makes code a bit easier to read.


Petr