Wednesday, August 2, 2017

UVa problem solution 11466 - Largest Prime Divisor

problem link:

Discuss: just find prime factor and replace maximum prime factor with largest one and if there is only one prime factor then print -1 otherwise print the largest one


try yourself before see the code



#include<bits/stdc++.h>
using namespace std;
int primefactor(long long n)
{
    long long i,j,cnt=0,maxx=2,m;
    m=n;
    if(m%2==0)
    {
           cnt++;
        while(m%2==0)
        {

           m=m/2;

        }
    }
    for(i=3;i<=sqrt(m);i+=2)
    {
          if(m%i==0)
          {
              cnt++;
            while(m%i==0)
            {
               m=m/i;
               if(i>maxx)
                maxx=i;
            }
          }
    }
    if(m>2)
    {
        cnt++;
       if(maxx<m)
        maxx=m;
    }
    if(cnt >1)
        cout <<maxx<<endl;
    else
    cout <<-1<<endl;
}
int main()
{

    long long n,i,j,sum;
    while(cin >>n&&n)
    {
        if(n<0)
        n*=-1;

        j=primefactor(n);
    }
}


No comments:

Post a Comment