求出2000以内的满足以下条件的正整数:该数本身不是素数,但他的所有因子之和是素数。

来源:百度知道 编辑:UC知道 时间:2024/06/14 15:55:53

Private Sub Command1_Click()
For i = 1 To 2000
If Prime(Sum_Y(i)) = True And Prime(i) = False Then Print i
Next
End Sub
'求素数函数
Function Prime(ByVal n As Integer) As Boolean
For i = 2 To Int(Sqr(n))
If n Mod i = 0 Then Prime = False: Exit Function
Next
Prime = True
End Function
'求因子和
Function Sum_Y(ByVal n As Integer) As Integer
Sum_Y = 1
For i = 2 To n
If n Mod i = 0 Then Sum_Y = Sum_Y + i: Sum_Y (n Mod i)
Next
End Function

Private Sub Command1_Click()
For i = 4 To 2000
s = 1
For j = 2 To i \ 2
If i Mod j = 0 Then s = s + j
Next j
If s > 1 Then
s = s + i
For k = 2 To Sqr(s)
If s Mod k = 0 Then
Exit For
End If
Next k
If k > Sqr(s) Then Print i
End If
Next i
End Sub