UVA 11466 Largest Prime Divisor solution

Solution in c++:
///**********ALLAH IS ALMIGHTY************///
///AH Tonmoy
///Department of CSE,23rd batch
///Islamic University,Bangladesh
#include <iostream>
#include<math.h>
#include<set>
using namespace std;
set<int>s;
long long maxPrimeFactors(long long n)
{

    long long maxPrime = -1;

    while (n % 2 == 0)
    {
        maxPrime = 2;
        s.insert(2);
        n >>= 1;
    }
    for (int i = 3; i <= sqrt(n); i += 2)
    {
        while (n % i == 0)
        {
            maxPrime = i;
            s.insert(i);
            n = n / i;

        }
    }

    if (n > 2)
    {
        maxPrime = n;
        s.insert(n);
    }
    return maxPrime;
}

int main()
{
    long long n,a,v;
    while(cin>>a)
    {
        if(a==0)
            break;
        else
        {
            n=abs(a);
            v=maxPrimeFactors(n);
            if((s.size()>1)&&n>5)
            {
                cout <<v << endl;
            }
            else
                cout<<"-1"<<endl;
        }
        s.clear();
    }
}

No comments

Most View Post

Recent post

Codeforces Round 971 (Div. 4) 2009C. The Legend of Freya the Frog Solution

  Problem Link    https://codeforces.com/contest/2009/problem/C S olution in C++: /// Author : AH_Tonmoy #include < bits / stdc ++. h &g...

Powered by Blogger.