首頁 > 後端開發 > C++ > 主體

C程式用來求出一個數的最大質因子

王林
發布: 2023-08-27 10:09:05
轉載
1476 人瀏覽過

C程式用來求出一個數的最大質因子

Prime Factor− In number theory, the prime factors of a positive integer are the prime numbers that divide that integer exactly. The process of finding these numbers is called integer factorization, or prime factorization.

Example− Prime factors of 288 are: 288 = 2 x 2 x 2 x 2 x 2 x 3 x 3

Input: n = 124
Output: 31 is the largest prime factor!
登入後複製

##Explanation

您將找到一個數字的所有質因數,並找出其中最大的質因數。 124的質因數為2 x 2 x 31,其中31為最大的質因數。

Example

#include <stdio.h>
int main() {
   long int n;
   n=3453;
   long int div=2, ans = 0, maxFact;
   while(n!=0) {
      if(n % div !=0)
         div = div + 1;
      else {
         maxFact = n;
         n = n / div;
         if(n == 1) {
            printf("%d is the largest prime factor !",maxFact);
            ans = 1;
            break;
         }
      }
   }
   return 0;
}
登入後複製

輸出

1151 is the largest prime factor !
登入後複製

以上是C程式用來求出一個數的最大質因子的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:tutorialspoint.com
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
最新問題
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!