下面的代码能处理 不大于 999999999(九个9)的自然数,速度比原来的快
Private Sub Command1_Click()
Dim 自然数 As Long
Dim B As Long
Dim 质数() As Long
Dim i As Long
Dim J As Long
Dim K As Long
Dim 质数表(41) As Long
质数表(1) = 2
质数表(2) = 3
质数表(3) = 5
质数表(4) = 7
质数表(5) = 11
质数表(6) = 13
质数表(7) = 17
质数表(8) = 19
质数表(9) = 23
质数表(10) = 29
质数表(11) = 31
质数表(12) = 37
质数表(13) = 41
质数表(14) = 43
质数表(15) = 47
质数表(16) = 53
质数表(17) = 59
质数表(18) = 61
质数表(19) = 67
质数表(20) = 71
质数表(21) = 73
质数表(22) = 79
质数表(23) = 83
质数表(24) = 89
质数表(25) = 97
质数表(26) = 101
质数表(27) = 103
质数表(28) = 107
质数表(29) = 109
质数表(30) = 113
质数表(31) = 127
质数表(32) = 127
质数表(33) = 131
质数表(34) = 137
质数表(35) = 139
质数表(36) = 151
质数表(37) = 157
质数表(38) = 163
质数表(39) = 167
质数表(40) = 173
Command1.Enabled = False
自然数 = Text2.Text
If 自然数 > 999999999 Then
MsgBox "嘿嘿,暂时不支持 自然数 > 999999999 (九个9)!"
Exit Sub
End If
K = 自然数
If 自然数 > 1000 Then '自然数>1000 ,这里可改合适些,以免较小的自然数循环判断过多
For i = 1 To 40
B = Fix(自然数 / 质数表(i))
If B * 质数表(i) = 自然数 Then
J = J + 1
ReDim Preserve 质数(1 To J) As Long
质数(J) = 质数表(i)
自然数 = B
End If
Next
End If
For i = 2 To 自然数
B = Fix(自然数 / i)
If B * i = 自然数 Then
J = J + 1
ReDim Preserve 质数(1 To J) As Long
质数(J) = i
自然数 = B
i = 1
End If
Next
Text1.Text = K & " = "
For i = 1 To J
If Text1.Text <> K & " = " Then Text1.Text = Text1.Text & " * "
Text1.Text = Text1.Text & 质数(i)
Next
Command1.Enabled = True
End Sub
Private Sub Text2_KeyPress(KeyAscii As Integer)
'只能输入整数
Select Case KeyAscii
Case Asc("0") To Asc("9"), vbKeyBack
'nop
Case Else
KeyAscii = 0
End Select
End Sub
菜鸟在渐渐变老,终有一天会成为老鸟, 并为自由翱翔作最后的准备!