Hallo Leute,
ich habe eine Aufgabe die da lautet 2 eingegebene Zahlen als Primzahlzwilling zu identifizieren.
Dabei soll die Prüfung auf Primzahl und auf Zwilling in einer Function geschehen.
Meine erste programmierte Idee sieht wie folgt aus:
Option Explicit
Sub Primzahl()
' variablen deklaration
Dim eingabe0 As Double
Dim eingabe1 As Double
Dim x0 As Double
Dim x1 As Double
'Inputbox für Eingabe + >1?
'eingabe = Application.InputBox(prompt:="Bitte geben Sie eine Zahl ein." & _
'"Wir werden sehen ob es sich um eine Primzahl handelt.", Type:=1)
eingabe0 = InputBox("Zahl 1 eingeben: ")
If eingabe0 <= 1 Then
MsgBox ("Nur Zahlen größer 1!")
Exit Sub
End If
eingabe1 = InputBox("Zahl 2 eingeben: ")
If eingabe1 <= 1 Then
MsgBox ("Nur Zahlen größer 1!")
Exit Sub
End If
' eingabe0 = Primzahl?
x0 = Prim((eingabe0))
' Ausgabe eingabe0
If x0 > 2 Then
MsgBox (eingabe0 & " ist keine Primzahl")
Else
MsgBox (eingabe0 & " ist Primzahl")
End If
' eingabe1 = Primzahl?
x1 = Prim((eingabe1))
' Ausgabe eingabe1
If x1 > 2 Then
MsgBox (eingabe1 & " ist keine Primzahl")
Else
MsgBox (eingabe1 & " ist Primzahl")
End If
If x0 <= 2 And x1 <= 2 Then
Call Zwilling(eingabe0, eingabe1)
End If
End Sub
Function Prim(Param As Double) As Double
Dim i As Double
Dim a As Double
Prim = 0
' Primzahlprüfung
For i = 1 To Param
a = Param / i
If a = Int(a) Then
Prim = Prim + 1
End If
Next
Exit Function
End Function
Function Zwilling(Param0 As Double, Param1 As Double) As Double
Zwilling = Param0 - Param1
If Zwilling = 2 Or Zwilling = -2 Then
MsgBox "Bei " & Param0 & " und " & Param1 & " handelt es sich um ein Primzahlzwilling"
End If
End Function
An sich funktioniert es, gibt es eventuell noch Verbessereungen die der Ein oder Andere hat?
mfg
septix