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

2023年 8月 27日 24.7k 0

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
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程序用于找到一个数的最大质因子的详细内容,更多请关注每日运维网(www.mryunwei.com)其它相关文章!

相关文章

JavaScript2024新功能:Object.groupBy、正则表达式v标志
PHP trim 函数对多字节字符的使用和限制
新函数 json_validate() 、randomizer 类扩展…20 个PHP 8.3 新特性全面解析
使用HTMX为WordPress增效:如何在不使用复杂框架的情况下增强平台功能
为React 19做准备:WordPress 6.6用户指南
如何删除WordPress中的所有评论

发布评论