Thursday, November 16, 2017

1050 - Coprimes COJ Problem Solution

http://coj.uci.cu/24h/problem.xhtml?pid=1050

In C++............

#include<iostream>
#include<cstdio>

using namespace std;

//Nayeem Shahriar Joy,Applied Physics &Electronic Engineering,University of Rajshahi.


int phie(int n)

{
    int result =n;
    for(int p=2;p*p<=n;++p)
    {
        if(n%p==0)
        {
            while(n%p==0)
            {
                n=n/p;
            }
            result=result-(result/p);
        }
    }
    if(n>1)
    {
        result=result-(result/n);
    }
    return result;
}

int main()

{
    int n;
    cin>>n;
    cout<<phie(n)<<endl;
}

No comments:

Post a Comment