题目描述
A prime number[质数] is a positive integer that has exactly two different positive divisors,1 and itself. For example, 2,3,5,7,11, and 13 are prime numbers. Write a C program to calculate the prime divisors[质因子] of the number n.
输入
The input will consist of several numbers, separated by a space. The first number represents the total number m(1<m<100) of the non-negative integers to process. The following numbers represents even integers ni(2<= ni <=10000000).
输出
Output the prime divisors.There are m results. Display one result per line.
样例输入
2 2 100
样例输出
2=2 100=2*2*5*5
在找一个数的质数因子过程中,其实就是在寻找他的因子过程。我们可以对它先找第一个整除数,对它相除然后赋值,比如15/3=5,25/5=5,100/2=50,然后继续寻找下一个整除数。有人可能会疑问不用考虑非质数的问题吗,其实是不用考虑的。因为每个非质数都可以被质数相除。直到寻找到最后一个质因子时,也就是本身,相除为1结束循环
#include<stdio.h>
int prime_divisors(int n)
{
for(int i=2;i<100000;i++)//找质数因子
{
if(n%i==0)
{
n=n/i;
printf("%d",i);
if(n!=1)
{
printf("*");
}
else
{
return 0;
}
return prime_divisors(n);
}
}
}
int main()
{
int n,num;
scanf("%d",&n);
for(int i=0;i<n;i++)
{
scanf("%d",&num);
printf("%d=",num);
prime_divisors(num);
printf("\n");
}
return 0;
}