#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define MAX_N 60000
#define MAX_ITEM 7500
int primes[MAX_ITEM]= {0},numbers[MAX_ITEM]= {0};
int gen_primes(int n,int *primes);
void gen_factors(int n,int m,int *primes,int *num);
void prints(int m,int *primes,int *num ,int n);
int main()
{
int m,n;
printf("please Enter N:");
scanf("%d",&n);
if(n<2||n>MAX_N)
{
printf("N must be between 2 and %d\n",MAX_N);
}
else
{
m=gen_primes(n,primes);
gen_factors(n,m,primes,numbers);
prints(m,primes,numbers,n);
}
return 0;
}
int gen_primes(int n,int *primes)
{
int m=0,i,k;
k=n;
for(i=2; i<k; i++)
{
int d=2;
while(d>=n)
{
if(i%d!=0)
d++;
}
primes[m]=i;
m++;
}
return m;
}
void gen_factors(int n,int m,int *primes,int *num)
{
int i,j=0,k;
for(i=1;i<=n;i++)//分解质数因子
{
k=i;
while(primes[j]<=k&&j<=m)//保证循环的质数比分解的数字小
{
if(k%primes[j]==0)
{
k/=primes[j];
num[j]++;
j=0;//返回重新循环
}
else
j++;
}
}
}
void prints(int m,int *primes,int *num,int n)
{
int i;
for(i=0;i<m;++i)
{
if(num[i]==0)
{}
if(num[i]>=2)
printf("%d^%d*",primes[i],num[i]);
if(num[i]==1&&i==m-1)
printf("%d",primes[i]);
if(num[i]==1&&i<m-1)
printf("%d*",primes[i]);
}
}
王朝 N的阶乘的质数因子分解
最新推荐文章于 2021-05-19 12:53:58 发布