HDU 2136 largest prime factor three methods for prime number table

Source: Internet
Author: User

Three methods for prime number table are summarized.

First:

void intline(){    memset(prime,true,sizeof(prime));    prime[1]=false;    prime[0]=false;    for(int i=2; i*i<MAXN; i++)    {        for(int j=2; j*i<MAXN; j++)            prime[j*i]=false;    }}

Second: similar to the first principle, it is also the method used for this question, but this is an array opened with an int, prime [I] = J indicates that the maximum prime factor of I is the prime number J, and 2 is the first prime number). Therefore, this method has many variants.

void intline(){    int num=0;   memset(prime,0,sizeof(prime));    for(int i=2;i<MAXN;i++)    {        if(!prime[i])        {            num++;            for(int j=i;j<MAXN;j+=i)                prime[j]=num;        }    }}

The third method is the most common method.

int isprime(int n){    for(int i=2; i*i<=n; i++)        if(n%i==0)  return 0;    return 1;}

Fourth type: in fact, like the second type, it is the first type of deformation. This prime [I] = J indicates that the prime number of I is J.

void intline(){    memset(num,0,sizeof(num));    num[0]=num[1]=1;    int cnt=0;    for(int i=2; i*i<MAXN; i++)    {        if(!num[i])        {            prime[cnt++]=i;            for(int j=2; j*i<MAXN; j++)                num[j*i]=1;        }    }}

Code for this question:

#include<cstdio>#include<cstring>#include<iostream>#define MAXN  1000001using namespace std;int prime[MAXN];void intline(){    int num=0;    for(int i=2;i<MAXN;i++)    {        if(!prime[i])        {            num++;            for(int j=i;j<MAXN;j+=i)                prime[j]=num;        }    }}int main(){    //freopen("in.txt","r",stdin);    intline();    int n;    while(scanf("%d",&n)!=EOF)    {        printf("%d\n",prime[n]);    }    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.