问题的提出:将一个整数分解素因数
根据算术基本定理,任何大于2的正整数都可以表示为素数的乘积,如果不考虑这些素数出现的次序,其
表示方式为唯一的。本题目的要求是输入一个正整数,将这个正整数显示为几个素数乘积的形式,如
如
1=1
2=2
3=3
4=2*2
5=5
6=2*3
7=7
...
60=2*2*3*5
#include <stdio.h>
#include <stdlib.h>
int f( int n, int a[])
{
int chushu;
int t;
if (n<4)
{
a[0]=n;
return 1;
}
t=0;
while(n%2==0)
{
a[t]=2;
t++;
n/=2;
}
chushu=3;
while (chushu*chushu<=n)
{
while (n % chushu==0)
{
a[t]=chushu;
t++;
n = n/chushu;
}
chushu=chushu+2;
}
if (n==1)
{
return t;
}
a[t++]=n;
return t;
}
int main()
{
int n,z[100],i,t;
scanf("%d",&n);
t=f(n,z);
printf("%d=%d",n,z[0]);
for (i=1;i<t;i++)
{
printf("*%d",z[i]);
}
return 0;
}