vb编程:找出仅由数字1、2、3、4组成的4位素数,要求每个素数由四个不同的数字组成

2024-11-27 00:46:33
推荐回答(2个)
回答1:

不错的问题,呵呵

在 1楼得 分析下,程序如下
Private Sub Command1_Click()
If IsPrime(4321) Then
Text1.Text = Text1.Text & "4321 Is Prime! " & vbCrLf
End If
If IsPrime(4231) Then
Text1.Text = Text1.Text & "4231 Is Prime! " & vbCrLf
End If
If IsPrime(3421) Then
Text1.Text = Text1.Text & "3421 Is Prime! " & vbCrLf
End If
If IsPrime(3241) Then
Text1.Text = Text1.Text & "3241 Is Prime! " & vbCrLf
End If
If IsPrime(2341) Then
Text1.Text = Text1.Text & "2341 Is Prime! " & vbCrLf
End If
If IsPrime(2431) Then
Text1.Text = Text1.Text & "2431 Is Prime! " & vbCrLf
End If

If IsPrime(4213) Then
Text1.Text = Text1.Text & "4213 Is Prime! " & vbCrLf
End If
If IsPrime(4123) Then
Text1.Text = Text1.Text & "4123 Is Prime! " & vbCrLf
End If
If IsPrime(2143) Then
Text1.Text = Text1.Text & "2143 Is Prime! " & vbCrLf
End If
If IsPrime(2413) Then
Text1.Text = Text1.Text & "2413 Is Prime! " & vbCrLf
End If
If IsPrime(1423) Then
Text1.Text = Text1.Text & "1423 Is Prime! " & vbCrLf
End If
If IsPrime(1243) Then
Text1.Text = Text1.Text & "1243 Is Prime! " & vbCrLf
End If

End Sub

Private Function IsPrime(Number As Integer) As Boolean
Dim i As Integer
IsPrime = True
For i = 2 To Sqr(Number)
If Number Mod i Then
Else
IsPrime = False
Exit Function
End If

Next i
End Function

回答2:

因为要求每个素数由四个不同的数字组成
所以这个四位数必须以1和3做为个位数
所以满足条件可能是素数的个数是12个
分别是:4321,4231,3421,3241,2431,2341;4213,4123,2413,2143,1423,1243
所以只用判断上述几个数是否是素数即可