|
VBA - Primzahlen ermitteln |
|
|
|
|
Geschrieben von: Marcus Rose
|
|
Dienstag, 13. Mai 2008 um 17:25 Uhr |
|
Bei diesem Makro könnt Ihr Zahlen in eine Inputbox eingeben, wo im Anschluß geprüft wird, ob diese Zahl eine Primzahl ist. Option Explicit Sub Primzahl() Dim x As Double Dim prim As Double Dim a As Double Dim b As Double b = 0 x = Application.InputBox(prompt:="Bitte geben Sie eine Zahl ein." & _ "Wir werden sehen ob es sich um eine Primzahl handelt.", Type:=1) If x = 1 Then MsgBox ("Eine Primzahl muß einen Wert über 1 haben") Exit Sub End If For prim = 1 To x a = x / prim If a = Int(a) Then b = b + 1 End If Next If b > 2 Then MsgBox (x & " ist keine Primzahl") Exit Sub Else MsgBox (x & " ist eine Primzahl") End If End Sub
|