VERSION 5.00
Begin VB.Form Form1 
   AutoRedraw      =   -1  'True
   BorderStyle     =   1  'Fixed Single
   Caption         =   "Form1"
   ClientHeight    =   3195
   ClientLeft      =   45
   ClientTop       =   330
   ClientWidth     =   4680
   LinkTopic       =   "Form1"
   MaxButton       =   0   'False
   MinButton       =   0   'False
   ScaleHeight     =   3195
   ScaleWidth      =   4680
   StartUpPosition =   3  '窗口缺省
   Begin VB.CommandButton Command2 
      Caption         =   "退出"
      Height          =   375
      Left            =   2880
      TabIndex        =   3
      Top             =   2760
      Width           =   855
   End
   Begin VB.CommandButton Command1 
      Caption         =   "计算"
      Height          =   375
      Left            =   1560
      TabIndex        =   2
      Top             =   2760
      Width           =   855
   End
   Begin VB.TextBox Text1 
      Height          =   270
      Left            =   2040
      TabIndex        =   1
      Top             =   1320
      Width           =   1095
   End
   Begin VB.Label Label2 
      AutoSize        =   -1  'True
      Height          =   180
      Left            =   1920
      TabIndex        =   4
      Top             =   1800
      Width           =   90
   End
   Begin VB.Label Label1 
      AutoSize        =   -1  'True
      Caption         =   "N="
      Height          =   180
      Left            =   1800
      TabIndex        =   0
      Top             =   1320
      Width           =   180
   End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
'Option ExplicitDim nCount As IntegerPrivate Sub Command1_Click()
Dim N As LongIf Text1 = "" Then
    N = 8
Else
    N = Val(Text1)
End IfCal_N NEnd SubPrivate Sub Command2_Click()
End
End SubPrivate Sub Form_Load()Form1.Width = 800 * 15
Form1.Height = 600 * 15
Form1.ScaleMode = vbPixels
Form1.Left = (Screen.Width - Form1.Width) / 2
Form1.Top = (Screen.Height - Form1.Height) / 2Command1.Left = 350
Command1.Top = 500
Command2.Left = 450
Command2.Top = 500
End Sub
Private Sub Cal_N(ByVal N As Long)
Dim i As Long
Dim j As Long
Dim k As Long
Dim X As Long
Dim Y As Long
Dim Z As Long
Dim xRangeMin As Long
Dim xRangeMax As Long
Dim yRangeMin As Long
Dim yRangeMax As Long
Dim Nxy As Long
Dim xy_N As LongnCount = 0
xRangeMax = N * 3 / 4   
xRangeMin = N / 4       For i = xRangeMin To xRangeMax
    If N <> 4 * i Then
        yRangeMax = i * N / (i + i + i + i - N) * 2
        'yRangeMin = i * N / (i + i + i + i - N)
    
        For j = i To yRangeMax
            Nxy = N * i * j
            xy_N = 4 * i * j - N * i - N * j
            If xy_N > 0 Then
    `            If Nxy Mod xy_N = 0 Then
                    Open "d:\N=" & N & ".txt" For Append As #1
                    Print #1, i & " " & j & " " & Nxy / xy_N & Chr(13)
                    Print i & " " & j & " " & Nxy / xy_N
                    Close #1
                    nCount = nCount + 1
                End If
            End If
        Next j
    End If
Next i
Label2.Caption = "总共得到" & nCount & "组结果。"
End Sub我要的是求出4/n=1/x+1/y+1/z的所有的正整数的解,x,y,z之间的排序算一个解,
例如:4/8=1/6+1/6+1/6=1/5+1/10+1/10=1/4+1/8+1/8=.....=....
这个n=8时有10组解;上面的程序我试n=500的行,而n=2000的时候回益出。请高手们,教我怎么改,谢谢!

解决方案 »

  1.   

    我不知道怎么弄了,我用的已经是long了!
      

  2.   

    Long 数据类型      Long(长整型)变量存储为 32 位(4 个字节)有符号的数值形式,其范围从 -2,147,483,648 到 2,147,483,647。Long 的类型声明字符为和号 (&)。
    你那个表达时:Nxy = N * i * j计算的结果是2,148,288,000当然要溢出!
      

  3.   

    Nxy = N * i * j改为:Nxy = N * i/1000 * j(1000这数值时衰变取得,你自己可以改,这样可以使计算结果在可表达的范围内,避免溢出);Print #1, i & " " & j & " " & Nxy / xy_N *1000 & Chr(13)
    Print i & " " & j & " " & Nxy / xy_N *1000另有一个建议:
    将Open "d:\N=" & N & ".txt" For Append As #1这一句放在循环体前,将Close #1放在循环体后面