Thursday, July 27, 2017

UVa problem solution 583 - Prime Factors

problem link:


Discuss: Just find all prime factor. you may have to face trouble with x but give time it's easy present


try your self before see the code


#include<bits/stdc++.h>
using namespace std;
int flag=0;
int pfactor(long long n)
{
    int i;
    while(n%2==0)
    {
         if(flag==1)
            printf(" x ");
        cout <<2;
        n/=2;
        flag=1;

    }
    for(i=3;i<=sqrt(n);i+=2)
    {
        while(n%i==0)
        {
            if(flag==1)
            printf(" x ");
            cout <<i;
            n/=i;
            flag=1;
        }
    }
    if(n>2)
    {
        if(flag==1)
        printf(" x ");
        cout <<n;
    }
}
int main()
{
    long long n,m,i,j;
    while(cin >>n)
    {
        flag=0;
        if(n==0)
            break;
            m=n;
              printf("%lld = ",m);
            if(n<0)
            {
                cout <<-1;
                flag=1;
                n*=-1;
            }

        pfactor(n);
        cout <<endl;
    }
    return 0;

}

No comments:

Post a Comment