Largest prime factor

Problem Description
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的倍数全置1,然后将3的倍数置2,……
View Code
 1 #include<iostream>
 2 using namespace std;
 3 #define N 1000001
 4 int prime[N];
 5 int main()
 6  {
 7       for(int j=0;j<N;j++)
 8          prime[j]=-1;
 9       int num=0;
10       for(int i=2; i<N; i++)
11       {
12         if(prime[i]==-1)
13          {
14             num++;
15             for(int j=i;j<N;j+=i) 
16                prime[j]=num;
17          }
18       }
19       int n;   
20    while(scanf("%d",&n)!=EOF)
21        { 
22            if(n==1)
23            cout<<"0"<<endl;
24            else 
25           cout<<prime[n]<<endl;
26        } 
27    return 0;
28  }

 

posted on 2012-04-27 16:56  L_S_X  阅读(473)  评论(0编辑  收藏  举报

导航