|
楼主 |
发表于 2014-1-18 20:18
|
显示全部楼层
以下改进后的代码解决了因数分解的问题,但仍然只能最大判断15位数,我的目标是更大的数,有人说19个连续的1组成的19位数是一个质数,而在此之下的3到18位连续的1组成的数都是合数,对此,只能验证一大半,遗憾。。。
Private Sub CommandButton1_Click()
Dim x&, i&, n#, j#, pd As Boolean, pdjg$
If TextBox1.Text = "" Then MsgBox "请输入要判断的自然数!", , "友情提示": Label3.Caption = "": Exit Sub
If IsNumeric(TextBox1.Text) = False Then MsgBox "请输入数字,中间不能有空格!", , "友情提示": TextBox1.Text = "": Label3.Caption = "": Exit Sub
If TextBox1.Text <> Int(TextBox1.Text) Then MsgBox "请输入整数!", , "友情提示": TextBox1.Text = "": Label3.Caption = "": Exit Sub
If TextBox1.Text < 2 Then MsgBox "请输入大于1的自然数!", , "友情提示": TextBox1.Text = "": Label3.Caption = "": Exit Sub
If TextBox1.Text > 999999999999999# Then MsgBox "数据过大,本程序最大可判断的15位数!", , "友情提示": TextBox1.Text = "": Label3.Caption = "": Exit Sub
n = TextBox1.Text
If n = 2 Then Label3.Caption = "质数": Exit Sub
pd = False
300:
If n ^ 0.5 = Int(n ^ 0.5) Then x = n ^ 0.5 Else x = Int(n ^ 0.5 + 1)
For i = 2 To x
If i <> 2 And i Mod 2 = 0 Then GoTo 100
If i <> 3 And i Mod 3 = 0 Then GoTo 100
If i <> 5 And i Mod 5 = 0 Then GoTo 100
If i <> 7 And i Mod 7 = 0 Then GoTo 100
j = n / i
If j = Int(j) Then pdjg = pdjg & i & "×": pd = True: GoTo 200
100:
Next
GoTo 400
200:
n = j: If n = 2 Then GoTo 400 Else GoTo 300
400:
Label3.Caption = "合数 " & pdjg & n
If pd = False Then Label3.Caption = "质数"
End Sub |
|