完全数 原

概念:

各个小于它的约数(真约数,列出某数的约数,去掉该数本身,剩下的就是它的真约数)的和等于它本身的自然数叫做完全数(Perfect number),又称完美数或完备数.

第一个完全数是6,它有约数1、2、3、6,除去它本身6外,其余3个数相加,1+2+3=6

第二个完全数是28,它有约数1、2、4、7、14、28,除去它本身28外,其余5个数相加,1+2+4+7+14=28

第三个完全数是496,有约数1、2、4、8、16、31、62、124、248、496,除去其本身496外,其余9个数相加,1+2+4+8+16+31+62+124+248=496

后面的完全数还有8128、33550336等等

VBA代码,超过100000就运行比较慢

主程序

Public iOut
Public C
Sub Main_Perfect_Number()
Dim n
Dim i
Dim t

n = 10000

iOut = 10
Perfect_Number_1 n           'Way 1

iOut = 10                    'Way 2
For i = 1 To n
    t = Timer
    Set C = New Collection     'Initial Factor
    Perfect_Number_2 1, i, i
    Sheets("Way2").Cells(10, 1) = (Timer - t)
Next i

iOut = 10                   'Way 3
Perfect_Number_3 n

End Sub


'Factor OutPut
Public Sub Facotr_OutPut(Num, S, Sht)
Dim i
iOut = iOut + 1
With Sheets(Sht)
    .Cells(iOut, 1) = Num         'Object->Perfect Number
    For i = 1 To S.Count
        .Cells(iOut, i + 1) = S.Item(i)   'Read Collection
    Next i
End With
End Sub

循环方法

Way1

'Perfect Number, Way 1
'Loop All the Factor

Sub Perfect_Number_1(ByVal Num)
Dim t
Dim iNum
Dim iFactor
Dim Tmp
Dim Count As Collection
t = Timer
For iNum = 2 To Num
    Set Count = New Collection     'Initial Factor
    Tmp = iNum                     'Initial Number
    For iFactor = 1 To iNum - 1           'Loop process Single Number
        If iNum Mod iFactor = 0 Then      'Identify whether Factor
            Count.Add iFactor             'Add into Collection
            Tmp = Tmp - iFactor           'Decrease Number
        End If
        If Tmp < 0 Then Exit For
    Next iFactor
    'check the result-----------------
    If Tmp = 0 Then
        Facotr_OutPut iNum, Count, "Way1"
    End If
    '----------------------------------
Next iNum
Sheets("Way1").Cells(10, 1) = (Timer - t)
End Sub

 Way3

'Perfect Number, Way 1
'Loop All the Factor
'Num alway no big than Num/2

Sub Perfect_Number_3(ByVal Num)
Dim t
Dim iNum
Dim iFactor
Dim Tmp
Dim Count As Collection
t = Timer
For iNum = 2 To Int(Num / 2)
    Set Count = New Collection     'Initial Factor
    Tmp = iNum                     'Initial Number
    For iFactor = 1 To iNum - 1           'Loop process Single Number
        If iNum Mod iFactor = 0 Then      'Identify whether Factor
            Count.Add iFactor             'Add into Collection
            Tmp = Tmp - iFactor           'Decrease Number
        End If
        If Tmp < 0 Then Exit For
    Next iFactor
    'check the result-----------------
    If Tmp = 0 Then
        Facotr_OutPut iNum, Count, "Way3"
    End If
    '----------------------------------
Next iNum
Sheets("Way3").Cells(10, 1) = (Timer - t)
End Sub

递归方法

'Perfect Number, Way 1
'Recursion
'>10000 will overflow

Sub Perfect_Number_2(iFactor, iNum, Num)
If iNum < 0 Then Exit Sub
If iFactor = Num Then       'Exit
    If iNum = 0 Then Facotr_OutPut Num, C, "Way2"
    Exit Sub
End If
If Num Mod iFactor = 0 Then
    C.Add iFactor
    Perfect_Number_2 iFactor + 1, iNum - iFactor, Num
Else
    Perfect_Number_2 iFactor + 1, iNum, Num
End If

End Sub

Python,运行速度更快

# -*- coding: utf-8 -*-
"""
Created on Tue Oct 31 16:07:05 2017

@author: ZHENGTE
"""

'''Perfect Number, which summary of factor=Number'''
import time

def PerfectNumber(num):
    l=[]
    tmp=num
    l.append(num)
    for j in range(1,num):      #Loop factor
        if num%j==0:            #Check factor
           l.append(j)          #Add into LIST
           tmp=tmp-j
           
    if tmp==0:                 #Perfect Number 
        print l                #Print LIST
        print '\n'
        
if __name__=='__main__':
    start=time.clock()
    for i in range(1,10000):     #Loop Number Range
        PerfectNumber(i)         #Use Define function
    print("spend",time.clock()-start)

 

    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞