VB:输入一个正整数N,把它分解成质因子相乘的形式

用VB语言,如36=1*2*2*3*3,要详细并且解释清楚.
2024-11-21 16:43:10
推荐回答(2个)
回答1:

Option Explicit

Private Sub Command1_Click()
Dim N%, i%
N = Text1
Label1 = Text1 & "=1"
Do
Call mini(N, i)
N = N / i: Label1 = Label1 & "*" & Str(i)
Loop While N > 2
End Sub

Private Sub mini(N%, i%)'求最小的质数。
For i = 2 To N
If N Mod i = 0 Then Exit For
Next i
End Sub

回答2:

递归:
Private Sub getA(N As Integer)
If N <= 3 Then
Text1.Text = Text1.Text & "*" & N
Else
For i = 2 To N / 2
If N Mod i = 0 Then
N = N / i
Text1.Text = Text1.Text & "*" & i
Exit For
End If
Next i
getA (N)
End If
End Sub

如下调用:
Text1.Text = "36 = 1"
getA 36