求2--1000中,每个数有多少个不同的质因子,并要求输出如下的式子:2=2 3=3 4=2*2 ...100=2*2*5*5 用记事本编程vb语言急用
来源:学生作业帮助网 编辑:六六作业网 时间:2024/11/24 00:58:06
求2--1000中,每个数有多少个不同的质因子,并要求输出如下的式子:2=2 3=3 4=2*2 ...100=2*2*5*5 用记事本编程vb语言急用
求2--1000中,每个数有多少个不同的质因子,并要求输出如下的式子:
2=2
3=3
4=2*2
...
100=2*2*5*5
用记事本编程
vb语言
急用
求2--1000中,每个数有多少个不同的质因子,并要求输出如下的式子:2=2 3=3 4=2*2 ...100=2*2*5*5 用记事本编程vb语言急用
Rem 大家看你一分也不给,都不来搭理你哈,小女子发扬一下助人为乐的奉献精神吧
Rem 窗体建一个Text1,一个Text2,MultiLine属性改为True,ScrollBars设置为2(垂直滚动条)
Option Explicit
Private Function isprime(Num As Long) As Boolean
If Num < 2 Then isprime = False:Exit Function
Dim i As Long
For i = 2 To Sqr(Num)
If (Num Mod i) = 0 Then
isprime = False
Exit Function
End If
Next i
isprime = True
End Function
Private Sub Form_Click()
Dim i As Long,j As Long,temp As Long,diff As Long
Dim primestr(2 To 1000) As String,nall(2 To 1000) As Integer,ndiff(2 To 1000) As Integer
'primestr存放质因子的分解因式,nall存放质因子总的数目,ndiff存放不同质因子的数目
Cls
Text1 = ""
Text2 = ""
For i = 2 To 1000
Print i & "=";
primestr(i) = i & "="
nall(i) = 0:ndiff(i) = 0
temp = i
If Not isprime(temp) Then
j = 2
diff = 0
Do While Not isprime(temp)
If temp Mod j = 0 Then
Print j & "*";
primestr(i) = primestr(i) & j & "*"
nall(i) = nall(i) + 1
If diff j Then ndiff(i) = ndiff(i) + 1:diff = j
temp = temp \ j
Else
Do
j = j + 1
Loop Until isprime(j)
End If
Loop
If isprime(temp) And temp j Then ndiff(i) = ndiff(i) + 1
Else
ndiff(i) = 1
End If
Print CStr(temp)
primestr(i) = primestr(i) & CStr(temp) & vbCrLf
nall(i) = nall(i) + 1
Next
'输出不同质因子的数目
'For i = 2 To 1000
'Text1 = Text1 & i & ":" & ndiff(i) & vbCrLf
'Next
'在Text2中输出分解因式,这个有点慢哈,1000个数太多了
'For i = 2 To 1000
'Text2 = Text2 & primestr(i)
'Next
End Sub