如果一个数字的所有因子(包括1,但不包括该数本身)之和等于该数字,那么这个数字就称为完全数。设计一个程序,求3-1000之间的所有完全数,要求打印其因子。求大侠帮助

解决方案 »

  1.   

    Private Sub Form_Load()
    Dim iCtr As Double
    Dim jCtr As Double
    Dim pCtr As String
    Dim sCtr As String
        For iCtr = 3 To 10000000
            jCtr = iCtr * iCtr
            sCtr = CSng(iCtr)
            If InStr(1, Right(jCtr, Len(sCtr)), iCtr) Then
                pCtr = pCtr & "," & iCtr
            End If
        Next iCtr
        pCtr = Right(pCtr, Len(pCtr) - 1)
        Debug.Print pCtr
    End Sub
      

  2.   

    Module Module1    Sub Main()
            Dim result = Enumerable.Range(3, 1000 - 3).
                Where(Function(x) Enumerable.Range(1, x \ 2).Where(Function(y) x Mod y = 0).Sum() = x).
                Select(Function(x) x)
            result.ToList().ForEach(Sub(x) Console.WriteLine(x))
        End SubEnd Module
      

  3.   


        Dim a As Integer, b As Integer, c As Integer, s As String
        For a = 3 To 1000
            c = 1: s = "1"
            For b = 2 To a \ 2
                If a Mod b = 0 Then
                    c = c + b
                    s = s & "," & b
                End If
            Next b
            If a = c Then Debug.Print Str(a), s
        Next a结果:
     6            1,2,3
     28           1,2,4,7,14
     496          1,2,4,8,16,31,62,124,248
      

  4.   

    6
    28
    496
    Press any key to continue . . .
      

  5.   


        Dim a As Integer, b As Integer, c As Integer, s As String
        For a = 2 To 1000
            c = 1: s = "1"
            For b = 2 To a \ 2
                If a Mod b = 0 Then c = c + b: s = s & "," & b
            Next b
            If a = c Then Debug.Print a, s
        Next a
      

  6.   

    Module Module1    Sub Main()
            Dim result = Enumerable.Range(3, 1000 - 3).
                Where(Function(x) Enumerable.Range(1, x \ 2).Where(Function(y) x Mod y = 0).Sum() = x).
                Select(Function(x) x & ": " & Enumerable.Range(1, x \ 2).
                                                        Where(Function(y) x Mod y = 0).
                                                        Select(Function(z) z.ToString()).
                                                        Aggregate(Function(current, serials) serials + " " + current))
            result.ToList().ForEach(Sub(x) Console.WriteLine(x))
        End SubEnd Module
      

  7.   

    6: 3 2 1
    28: 14 7 4 2 1
    496: 248 124 62 31 16 8 4 2 1
    Press any key to continue . . .