Results 1 to 5 of 5

Thread: prime numbers (1-1000)

  1. #1
    Senior Member Lionheart008's Avatar
    Join Date
    Sep 2008
    Location
    Germany, Bad Sooden-Allendorf
    Age
    51
    Posts
    934
    Rep Power
    109

    prime numbers (1-1000)

    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).

    [code=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[/code]

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

    best regards, frank
    Attached Images Attached Images
    Attached Files Attached Files
    you can't always get what you want, but if you try sometimes you might find, you get what you need

  2. #2

    Re: prime numbers (1-1000)

    Nice example. Thanks for sharing!

  3. #3
    Super Moderator Petr Schreiber's Avatar
    Join Date
    Aug 2005
    Location
    Brno - Czech Republic
    Posts
    7,128
    Rep Power
    732

    Re: prime numbers (1-1000)

    Hi Frank,

    thanks!
    Little tip for you - instead of:
    [code=thinbasic]
    Begin Const, %WM_USER
    %listy
    %labely
    %texty
    End Const
    [/code]

    you can use more self documenting:
    [code=thinbasic]
    Begin ControlID
    %listy
    %labely
    %texty
    End ControlID
    [/code]

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


    Thanks!,
    Petr
    Learn 3D graphics with ThinBASIC, learn TBGL!
    Windows 10 64bit - Intel Core i5-3350P @ 3.1GHz - 16 GB RAM - NVIDIA GeForce GTX 1050 Ti 4GB

  4. #4
    Senior Member Lionheart008's Avatar
    Join Date
    Sep 2008
    Location
    Germany, Bad Sooden-Allendorf
    Age
    51
    Posts
    934
    Rep Power
    109

    Re: prime numbers (1-1000)

    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
    you can't always get what you want, but if you try sometimes you might find, you get what you need

  5. #5
    Super Moderator Petr Schreiber's Avatar
    Join Date
    Aug 2005
    Location
    Brno - Czech Republic
    Posts
    7,128
    Rep Power
    732

    Re: prime numbers (1-1000)

    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
    Learn 3D graphics with ThinBASIC, learn TBGL!
    Windows 10 64bit - Intel Core i5-3350P @ 3.1GHz - 16 GB RAM - NVIDIA GeForce GTX 1050 Ti 4GB

Similar Threads

  1. prime numbers spiral
    By zak in forum Math: all about
    Replies: 4
    Last Post: 08-06-2010, 09:02

Members who have read this thread: 0

There are no members to list at the moment.

Posting Permissions

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