Largest prime factor
题目:
Everybody knows any number can be combined by the prime number.
Now, your task is telling me what position of the largest prime factor.
The position of prime 2 is 1, prime 3 is 2, and prime 5 is 3, etc.
Specially, LPF(1) = 0.
Input
Each line will contain one integer n(0 < n < 1000000).
Output
Output the LPF(n).
Sample Input
1
2
3
4
5
Sample Output
0
1
2
1
3
本题题意是查找输入的数字组成其最大的素数所在素数表的位置,素数表:2,3,5,7,11等等。 如2在素数表的1号位置,3在素数表的2号位置以此类推,由此我们可用素数筛法解决, 代码如下:
#include<stdio.h>
int prim[1000005]={0};
void init()
{
int k;
prim[1]=0; k=1;
for(int i=2;i<1000000;i++)
if(prim[i]==0)//i是素數,因为每次前面j里面的循环j+=i都将包含前面素数的数组的值置为素数的位置
{
for(int j=i; j<1000000; j+=i)//j+i的意思是每次加i,这样后面是素数i的倍数的值代表的数组值为i所在素数表的位置
prim[j]=k;
k++;
}
}
int main()
{
int n;
init();
while(scanf("%d",&n)>0)
printf("%d\n",prim[n]);
}