Primzahlen

  • Hallo,

    ich würde gerne ein Programm schreiben, welches Primzahlen Listet!
    Nur , wie geht dies?

    Lg Pceumel

    Einmal editiert, zuletzt von pceumel (17. März 2009 um 12:14)

    • Offizieller Beitrag

    Suchfunktion benutzen .
    PrimA - der Primzahlengenerator

    • Offizieller Beitrag

    Das gab es hier schonmal.
    Hier mal drei Kandidaten (Bernd670, Anno2008 und progandy) und ihre Scripte:

    Spoiler anzeigen
    [autoit]


    ;===============================================================================
    $timer = TimerInit()
    $o = 100000
    $out = ''
    For $i = 2 To $o Step 1
    If IsPrime($i) Then $out &= $i & @CRLF
    Next
    $diff = TimerDiff($timer)
    _StringDisplay(Round($diff/1000, 3) & ' sek.' & @CRLF & $Out, 'Funktion von Bernd670')

    [/autoit] [autoit][/autoit] [autoit]

    Func IsPrime($iValue)
    $maxText = Sqrt($iValue)
    For $i = 2 To $maxText
    If Mod($iValue, $i) = 0 Then Return False
    Next
    Return True
    EndFunc ;==>IsPrime
    ;===============================================================================

    [/autoit] [autoit][/autoit] [autoit][/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]

    ;===============================================================================
    $timer = TimerInit()
    Global Const $max = 100001
    Global $string = ''
    Dim $prim[$max + 1]

    [/autoit] [autoit][/autoit] [autoit]

    For $i = 1 To $max - 1
    $prim[$i - 1] = False
    Next

    [/autoit] [autoit][/autoit] [autoit]

    For $i = 2 To $max / 2
    For $j = 2 To $max / $i
    $prim[$i * $j] = True
    Next
    Next

    [/autoit] [autoit][/autoit] [autoit]

    For $i = 2 To $max
    If Not $prim[$i] Then $string &= $i & @CRLF
    Next

    [/autoit] [autoit][/autoit] [autoit]

    $diff = TimerDiff($timer)
    _StringDisplay(Round($diff/1000, 3) & ' sek.' & @CRLF & $string, 'Funktion von Anno2008')
    ;===============================================================================

    [/autoit] [autoit][/autoit] [autoit][/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]

    ;===============================================================================
    $timer = TimerInit()
    $o = 100000
    $out = ''
    For $i = 2 To $o Step 1
    If IsPrime2($i) Then $out &= $i & @CRLF
    Next
    $diff = TimerDiff($timer)
    _StringDisplay(Round($diff/1000, 3) & ' sek.' & @CRLF & $Out, 'Funktion von progandy')

    [/autoit] [autoit][/autoit] [autoit]

    Func IsPrime2($iValue)
    Switch IsNumber($iValue)
    Case False
    $iValue = Number($iValue)
    EndSwitch
    Switch True
    Case $iValue = 2
    Return True
    Case $iValue < 2 Or Mod($iValue,2) = 0 Or IsFloat($iValue)
    Return False
    EndSwitch
    $maxTest = Floor(Sqrt($iValue))
    For $i = 3 To $maxTest Step 2
    If Mod($iValue, $i) = 0 Then Return False
    Next
    Return True
    EndFunc ;==>IsPrime2
    ;===============================================================================

    [/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]

    ;===============================================================================
    Func _StringDisplay($sText, $sTitle = 'StringDisplay', $sEditStyle = -1, $iWidth = 400, $iHeight = 300, $iLeft = -1, $iTop = -1)
    If Not IsDeclared('BS_DEFPUSHBUTTON') Then Local Const $BS_DEFPUSHBUTTON = 0x00000001
    If Not IsDeclared('GUI_EVENT_CLOSE') Then Local Const $GUI_EVENT_CLOSE = 0xFFFFFFFD
    If Not IsDeclared('WS_EX_COMPOSITED') Then Local Const $WS_EX_COMPOSITED = 0x02000000
    If Not IsDeclared('WS_MAXIMIZEBOX') Then Local Const $WS_MAXIMIZEBOX = 0x00010000
    If Not IsDeclared('WS_MINIMIZEBOX') Then Local Const $WS_MINIMIZEBOX = 0x00020000
    If Not IsDeclared('WS_SIZEBOX') Then Local Const $WS_SIZEBOX = 0x00040000
    Local $iEventMode = Opt('GUIOnEventMode', 0)
    Local $hGui = GUICreate($sTitle, $iWidth, $iHeight, $iLeft, $iTop, BitOR($WS_MINIMIZEBOX, $WS_MAXIMIZEBOX, $WS_SIZEBOX), $WS_EX_COMPOSITED)
    Local $hEdit = GUICtrlCreateEdit($sText, 5, 5, $iWidth - 10, $iHeight - 65, $sEditStyle)
    GUICtrlSetResizing(-1, 2 + 4 + 32 + 64)
    Local $hClose = GUICtrlCreateButton('Close', $iWidth / 2 - 25, $iHeight - 55, 50, 22, $BS_DEFPUSHBUTTON)
    GUICtrlSetResizing(-1, 64 + 256 + 512)
    ControlFocus($hGui, '', $hClose)
    GUISetState(@SW_SHOW, $hGui)
    While True
    Switch GUIGetMsg()
    Case $hClose, $GUI_EVENT_CLOSE
    ExitLoop
    EndSwitch
    WEnd
    ControlFocus($hGui, '', $hEdit)
    Local $sSelectedText = ControlCommand($hGui, '', $hEdit, 'GetSelected', '')
    If @error Then $sSelectedText = ''
    GUIDelete($hGui)
    Opt('GUIOnEventMode', $iEventMode)
    Return $sSelectedText
    EndFunc ;==>_StringDisplay

    [/autoit]