Hdustm6largest prime factor)

Source: Internet
Author: User

Problem DescriptionEverybody 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.

InputEach line will contain one integer n (0 <n <1000000 ).

OutputOutput the LPF (n ).

Sample Input

12345

Sample Output
01213 question: in the simplest form of a number, the maximum prime factor is located in all prime numbers, starting from 0. Analysis: All numbers can be expressed by multiples of prime numbers. The reason is that there are two types of numbers: prime numbers and non-prime numbers. If the prime number is not a prime number, it can be decomposed to obtain the simplest form, and the prime number cannot be decomposed. The simplest form is itself. To sum up, the factor in the simplest form of A number must be a prime number. Method 1:
# Include
  
   
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 is a prime number {for (int j = I; j <1000000; j + = I) prim [j] = k; k ++ ;}} int main () {int n; init (); while (scanf ("% d", & n)> 0) printf ("% d \ n", prim [n]);}
  


Method 2:
#include
  
   int vist[1000005]={0},prim[1000005];void init(){    int k;    prim[1]=1; prim[2]=2;k=3;    for(int i=3;i<1000000;i+=2)    if(vist[i]==0)    {        prim[i]=k; k++;        for(int j=i; j<1000000; j+=i)        if(vist[j]==0)        vist[j]=1;    }}int main(){    int n,posit;    init();    while(scanf("%d",&n)>0)    {        posit=1;        for(int i=2; ; i++)            if(prim[n]==0)            {                if(prim[i]&&n%i==0)posit=i;                while(n%i==0)n/=i;            }            else            {               if(n>posit)posit=n; break;            }            printf("%d\n",prim[posit]-1);    }}
  


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.