Clever use of prime sieve 1

Source: Internet
Author: User

Description
Everybody knows any number can is combined by the prime number.
Now, your task was telling me what position of the largest prime factor.
The position of Prime 2 is 1, Prime 3 are 2, and Prime 5 is 3, etc.
Specially, LPF (1) = 0.

Input
Each line would 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

 #include <iostream> #include <cstdio> #include <cstring> #include <
Algorithm> #include <cmath> using namespace std;
int prime[1000001];
BOOL isprime[1000001];
int p;
struct node//completely can not use, just lazy don't want to change {int pos;} pp[1000009];
    void sieve () {int i,j;
    memset (isprime,true,sizeof (IsPrime));
    Isprime[0]=isprime[1]=false;
            for (i=2; i<=1000001; i++) {if (Isprime[i]) {pp[i].pos=p;
            Prime[p++]=i; The maximum prime factor for the for (j=2*i; j<=1000000; j+=i) {pp[j].pos=pp[i].pos;//j is I isprime[j]
            =false;
}}} Isprime[1]=true;
    } int main () {int n;
    P=1;
    Prime[0]=1;
    Sieve ();
    pp[1].pos=0;
    while (scanf ("%d", &n)!=eof) {printf ("%d\n", Pp[n].pos);
} return 0; }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.