Sunday, September 10, 2017

10179 - Irreducable Basic Fractions

/***

            Bismillahir Rahmanir Rahim
            Read the name of Allah, who created you!!!
            Author : Shah Newaj Rabbi Shishir
            Department of CSE, City University, Bangladesh.

***/

#include <bits/stdc++.h>
using namespace std;

#define sf scanf
#define pf printf
#define scase sf ("%d",&tc)
#define sn sf ("%d",&n)
#define whilecase while (tc--)
#define eof while (cin >> n)
#define forloop for (pos=1; pos<=tc; pos++)
#define arrayloop (i=0; i<n; i++)
#define cinstr cin >> str
#define getstr getline (cin,str)
#define pcase pf ("Case %d: ",pos)

int totient (int n)
{
    int tot,i;

    if (n == 1)
        return 1;

    tot = n;

    if (!(n & 1))
    {
        tot = tot/2;

        while (!(n & 1))
            n /= 2;
    }

    for (i=3; i*i<=n; i+=2)
    {
        if (n % i == 0)
        {
            tot *= 1 - 1/(double)i;
            // tot -= tot/i;

            while (n % i == 0)
                n /= i;
        }
    }

    if (n > 1)
    {
        tot *= 1 - 1/(double)n;
        //tot -= tot/n;
    }

    return tot;
}

int main (void)
{
    int num;

    while (cin >> num && num)
        cout << totient(num) << endl;

    return 0;
}

No comments: