请用vb编写一个找出10000以内的亲密数对的小程序

2025-01-05 13:15:20
推荐回答(3个)
回答(1):

需要两个函数,一个因子和计算,一个因子和输出。程序窗体放置一个文本框,设置其MultiLine为True。源程序如下:

Option Explicit
Dim I As Long, J As Long, X As Long

Private Sub Form_Load()
Me.Show
For I = 1 To 10000
DoEvents
X = Yzh(I)
If X <= 10000 And I < X Then
If Yzh(X) = I Then
Text1 = Text1 & "(" & I & "," & X & ")" & Chr(13) & Chr(10)
YzhOut (I)
YzhOut (X)
End If
End If
Next
Text1 = Text1 & "计算完成"
End Sub

Private Function Yzh(N As Long) As Long
Yzh = 0
For J = 1 To N / 2
If N Mod J = 0 Then Yzh = Yzh + J
Next
End Function

Private Function YzhOut(N As Long) As Long
Dim Yzh As Long
Text1 = Text1 & N & "=1"
Yzh = 1
For J = 2 To N / 2
If N Mod J = 0 Then
Text1 = Text1 & "+" & J
Yzh = Yzh + J
End If
Next
Text1 = Text1 & "=" & Yzh & Chr(13) & Chr(10)
End Function

程序计算结果,有5组亲密数对,程序输出是:
(220,284)
220=1+2+4+5+10+11+20+22+44+55+110=284
284=1+2+4+71+142=220
(1184,1210)
1184=1+2+4+8+16+32+37+74+148+296+592=1210
1210=1+2+5+10+11+22+55+110+121+242+605=1184
(2620,2924)
2620=1+2+4+5+10+20+131+262+524+655+1310=2924
2924=1+2+4+17+34+43+68+86+172+731+1462=2620
(5020,5564)
5020=1+2+4+5+10+20+251+502+1004+1255+2510=5564
5564=1+2+4+13+26+52+107+214+428+1391+2782=5020
(6232,6368)
6232=1+2+4+8+19+38+41+76+82+152+164+328+779+1558+3116=6368
6368=1+2+4+8+16+32+199+398+796+1592+3184=6232
计算完成

回答(2):

需要两个函数,一个因子和计算,一个因子和输出。程序窗体放置一个文本框,设置其MultiLine为True。源程序如下:
Option
Explicit
Dim
I
As
Long,
J
As
Long,
X
As
Long
Private
Sub
Form_Load()
Me.Show
For
I
=
1
To
10000
DoEvents
X
=
Yzh(I)
If
X
<=
10000
And
I
<
X
Then
If
Yzh(X)
=
I
Then
Text1
=
Text1
&
"("
&
I
&
","
&
X
&
")"
&
Chr(13)
&
Chr(10)
YzhOut
(I)
YzhOut
(X)
End
If
End
If
Next
Text1
=
Text1
&
"计算完成"
End
Sub
Private
Function
Yzh(N
As
Long)
As
Long
Yzh
=
0
For
J
=
1
To
N
/
2
If
N
Mod
J
=
0
Then
Yzh
=
Yzh
+
J
Next
End
Function
Private
Function
YzhOut(N
As
Long)
As
Long
Dim
Yzh
As
Long
Text1
=
Text1
&
N
&
"=1"
Yzh
=
1
For
J
=
2
To
N
/
2
If
N
Mod
J
=
0
Then
Text1
=
Text1
&
"+"
&
J
Yzh
=
Yzh
+
J
End
If
Next
Text1
=
Text1
&
"="
&
Yzh
&
Chr(13)
&
Chr(10)
End
Function
程序计算结果,有5组亲密数对,程序输出是:
(220,284)
220=1+2+4+5+10+11+20+22+44+55+110=284
284=1+2+4+71+142=220
(1184,1210)
1184=1+2+4+8+16+32+37+74+148+296+592=1210
1210=1+2+5+10+11+22+55+110+121+242+605=1184
(2620,2924)
2620=1+2+4+5+10+20+131+262+524+655+1310=2924
2924=1+2+4+17+34+43+68+86+172+731+1462=2620
(5020,5564)
5020=1+2+4+5+10+20+251+502+1004+1255+2510=5564
5564=1+2+4+13+26+52+107+214+428+1391+2782=5020
(6232,6368)
6232=1+2+4+8+19+38+41+76+82+152+164+328+779+1558+3116=6368
6368=1+2+4+8+16+32+199+398+796+1592+3184=6232
计算完成

回答(3):

呵呵,用枚举做咯
对每个数都检查一遍看是否符合要求

不过要求一个数的因子和,这个函数我还真的不会