求一个数的素数生成法!!

来源:百度知道 编辑:UC知道 时间:2024/06/02 15:09:36
输入一个数n( n<2^32 ),输入n的素数相乘。
例如
Key #1: 195 = 3 x 5 x 13
Key #2: 196 = 2 x 2 x 7 x 7
Key #3: 197 = 197
Key #4: 198 = 2 x 3 x 3 x 11
Key #5: 199 = 199
Key #6: 200 = 2 x 2 x 2 x 5 x 5

前面的不要管了,就是不知道后面这个素数生成法怎么做,高手帮帮忙吧。
还是要超时啊。还得优化一下。

就是分解质因数

#include<stdio.h>

int main(void)
{
long i, n;

printf("input a number: ");
scanf("%ld", &n);
printf("%ld=", n);

for(i = 2; i < n;)
{
if (n % i != 0)
{
i++;
}
else
{
printf("%ld*", i);
n /= i;
i = 2;
}
}
printf("%ld",i);

}

#include<stdio.h>

int main(void)
{long i, n;
printf("input a number: ");
scanf("%ld", &n);
printf("%ld=", n);

for(i = 2; i < n;)
{if (n % i != 0)
{i++;}
else
{printf("%ld*", i);
n /= i;
i = 2;}
}
printf("%ld",i);

oj1502 普通质数打表,加一个素性检测