文章編號: 202782 - 上次校閱: 2006年10月10日 - 版次: 2.2

如何取得質數或因素

系統提示本文適用於您使用的作業系統之外的作業系統。與您不相關的文章內容已停用。
本文也適用於:
  • Windows 95 7.0|7.0 的 Microsoft Excel
  • Windows 95 7.0a|7.0a 的 Microsoft Excel
  • Windows 5.0|5.0 的 Microsoft Excel
  • Microsoft Excel 的 Windows 5.0c|5.0 c
  • Microsoft Excel 的 Macintosh 5.0|5.0
  • Microsoft Excel 的 Macintosh 5.0a|5.0a
全部展開 | 全部摺疊

結論

質數的數字是任何可以平均只能由本身劃分的整數,另一個。數字因數是一個可以被平均分割成該數字的整數。這份文件會包含應用程式的巨集的 Microsoft Visual Basic,您可以使用判斷在範圍內的所有質數或衍生的所有數字的因素的 Excel。

其他相關資訊

Microsoft 僅,為了說明提供程式設計範例,不提供任何明示或默示的保證。這包括,但不限於適售性或適合某特定用途之默示擔保責任。本文假設您已熟悉使用我們所示範的程式設計語言以及建立和偵錯程序所使用的工具。Microsoft 技術支援工程師可以協助解釋特定程序的功能,但它們不會修改這些範例以提供附加功能或建構程序,以符合您特定需求。若要建立巨集,請依照下列步驟執行:

  1. 儲存並關閉任何開啟的活頁簿,然後再建立新的活頁簿。
  2. 建立新模組:

    在 Microsoft Excel 97 中啟動 「 Visual Basic 編輯程式 」 藉由按下 ALT + F11

    在 Microsoft Excel 5.0] 或 [7.0 中按一下 [插入] 功能表上的 [巨集] 及 [模組]。
  3. 在模組中輸入下列程式碼:
       Sub GetFactors()
          Dim Count As Integer
          Dim NumToFactor As Single 'Integer limits to < 32768
          Dim Factor As Single
          Dim y As Single
          Dim IntCheck As Single
       
          Count = 0
          Do
             NumToFactor = _
                Application.InputBox(Prompt:="Type integer", Type:=1)
             'Force entry of integers greater than 0.
             IntCheck = NumToFactor - Int(NumToFactor)
             If NumToFactor = 0 Then
                Exit Sub
                'Cancel is 0 -- allow Cancel.
             ElseIf NumToFactor < 1 Then
                MsgBox "Please enter an integer greater than zero."
             ElseIf IntCheck > 0 Then
                MsgBox "Please enter an integer -- no decimals."
             End If
             'Loop until entry of integer greater than 0.
          Loop While NumToFactor <= 0 Or IntCheck > 0
          For y = 1 To NumToFactor
             'Put message in status bar indicating the integer being checked.
             Application.StatusBar = "Checking " & y
             Factor = NumToFactor Mod y
             'Determine if the result of division with Mod is without _
                 remainder and thus a "factor".
             If Factor = 0 Then
                'Enter the factor into a column starting with the active cell.
                ActiveCell.Offset(Count, 0).Value = y
                'Increase the amount to offset for next value.
                Count = Count + 1
             End If
          Next
          'Restore Status Bar.
          Application.StatusBar = "Ready"
       End Sub
       
       Sub GetPrime()
          Dim Count As Integer
          Dim BegNum As Single  'Integer limits to < 32768
          Dim EndNum As Single
          Dim Prime As Single
          Dim flag As Integer
          Dim IntCheck As Single
          Count = 0
       
          Do
             BegNum = _
                Application.InputBox(Prompt:="Type beginning number.", Type:=1)
             'Force entry of integers greater than 0.
             IntCheck = BegNum - Int(BegNum)
             If BegNum = 0 Then
                Exit Sub
                'Cancel is 0 -- allow Cancel.
             ElseIf BegNum < 1 Then
                MsgBox "Please enter an integer greater than zero."
             ElseIf IntCheck > 0 Then
                MsgBox "Please enter an integer -- no decimals."
             End If
             'Loop until entry of integer greater than 0.
          Loop While BegNum <= 0 Or IntCheck > 0
       
          Do
             EndNum = _
                Application.InputBox(Prompt:="Type ending number.", Type:=1)
             'Force entry of integers greater than 0.
             IntCheck = EndNum - Int(EndNum)
             If EndNum = 0 Then
                Exit Sub
                'Cancel is 0 -- allow Cancel.
             ElseIf EndNum < BegNum Then
                MsgBox "Please enter an integer larger than " & BegNum
             ElseIf EndNum < 1 Then
                MsgBox "Please enter an integer greater than zero."
             ElseIf IntCheck > 0 Then
                MsgBox "Please enter an integer -- no decimals."
             End If
             'Loop until entry of integer greater than 0.
          Loop While EndNum < BegNum Or EndNum <= 0 Or IntCheck > 0
       
          For y = BegNum To EndNum
             flag = 0
             z = 1
             Do Until flag = 1 Or z = y + 1
                'Put message into Status Bar indicating the integer and _
                    divisor in each loop.
                Application.StatusBar = y & " / " & z
                Prime = y Mod z
                If Prime = 0 And z <> y And z <> 1 Then
                   flag = 1
                End If
                z = z + 1
             Loop
       
             If flag = 0 Then
                'Enter the factor into a column starting with the active cell.
                ActiveCell.Offset(Count, 0).Value = y
                'Increase the amount to offset for next value.
                Count = Count + 1
             End If
          Next y
          'Restore Status Bar.
          Application.StatusBar = "Ready"
       End Sub
    					
  4. 在 [檔案] 功能表上按一下 [關閉],並返回 [Microsoft Excel]。切換至您想要執行該巨集的工作表,按一下您想要建立的因素或質數的資料行的方格。
  5. 在 [工具] 功能表上指向 [巨集,然後按一下 [巨集]。
  6. 按一下 GetFactors,然後再按一下 [執行] 以取得因素。

    -或者-

    按一下 GetPrime,然後再按一下 [執行] 以取得質數的清單。
注意: 雖然這些巨集可以找到質數大範圍的數字與大量的因素,執行這類程式碼可能需要很長的時間。基於這個理由狀態列上顯示進度時執行此巨集。

?考

如需有關取得應用程式的說明與 Visual Basic 的詳細資訊,請按一下下面的文件編號,檢視 「 Microsoft 知識庫 」 中的發行項:
163435? (http://support.microsoft.com/kb/163435/EN-US/ ) VBA: 應用程式的 Visual Basic 程式設計資源
226118? (http://support.microsoft.com/kb/226118/EN-US/ ) OFF2000: 應用程式的 Visual Basic 程式設計資源
如需有關執行範例程式碼的詳細資訊,請按一下下面的文件編號,檢視 「 Microsoft 知識庫 」 中的發行項:
173707? (http://support.microsoft.com/kb/173707/EN-US/ ) OFF97: 如何執行範例程式碼從眭舑恅梒
212536? (http://support.microsoft.com/kb/212536/EN-US/ ) OFF2000: 如何執行範例程式碼從眭舑恅梒

這篇文章中的資訊適用於:
  • Microsoft Excel 97 Standard Edition
關鍵字:?
kbmt kbhowto KB202782 KbMtzh
機器翻譯機器翻譯
重要:本文是以 Microsoft 機器翻譯軟體翻譯而成,而非使用人工翻譯而成。Microsoft 同時提供使用者人工翻譯及機器翻譯兩個版本的文章,讓使用者可以依其使用語言使用知識庫中的所有文章。但是,機器翻譯的文章可能不盡完美。這些文章中也可能出現拼字、語意或文法上的錯誤,就像外國人在使用本國語言時可能發生的錯誤。Microsoft 不為內容的翻譯錯誤或客戶對該內容的使用所產生的任何錯誤或損害負責。Microsoft也同時將不斷地就機器翻譯軟體進行更新。
按一下這裡查看此文章的英文版本:202782? (http://support.microsoft.com/kb/202782/en-us/ )
Microsoft及(或)其供應商不就任何在本伺服器上發表的文字資料及其相關圖表資訊的恰當性作任何承諾。所有文字資料及其相關圖表均以「現狀」供應,不負任何擔保責任。Microsoft及(或)其供應商謹此聲明,不負任何對與此資訊有關之擔保責任,包括關於適售性、適用於某一特定用途、權利或不侵權的明示或默示擔保責任。Microsoft及(或)其供應商無論如何不對因或與使用本伺服器上資訊或與資訊的實行有關而引起的契約、過失或其他侵權行為之訴訟中的特別的、間接的、衍生性的損害或任何因使用而喪失所導致的之損害、資料或利潤負任何責任。
Retired KB Article依現狀不再更新的知識庫內容免責聲明
本文旨在說明 Microsoft 不再提供支援的產品。因此,本文係依「現狀」提供,不會再更新。