C 程序查找数字的最大质因子
质因子− 在数论中,正整数的质因子是能整除该整数的质数。该过程被称为整数分解或质因子分解。
示例− 288 的质因子:288 = 2 x 2 x 2 x 2 x 2 x 3 x 3
Input: n = 124 Output: 31 is the largest prime factor!
解释
可以找出某个数的所有质因子,并找出其中最大的。124 的质因子为:2 x 2 x 31,而 31 是其中最大的。
示例
#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 !
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
安卓
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP