卓越飞翔博客卓越飞翔博客

卓越飞翔 - 您值得收藏的技术分享站
技术文章16333本站已运行3317

C程序用于找到一个数的最大质因子

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 !

卓越飞翔博客
上一篇: 深入学习Golang与百度AI接口:掌握图像分析技术
下一篇: 如何处理C++大数据开发中的数据聚类问题?
留言与评论(共有 0 条评论)
   
验证码:
隐藏边栏