UVA 10179 Irreducible Basic Fractions





Hints : Apply Euler Totient Function Rule

f(n) = n*[(1-(1/p1))*(1-(1/p2))*..........(1-(1/pk))]



#include <bits/stdc++.h>
using namespace std;
#define n 1000005
bool a[n];
long long int k=2;
long long int twin[n];
void sieve()
{
    long long int i,j;
    a[0]=a[1]=1;
    for(i=4;i<=n;i=i+2)
    {
        a[i]=1;
    }
    for(i=3;i<=sqrt(n);i=i+2)
    {
        for(j=i*i;j<=n;j=j+2*i)
        {
            a[j]=1;        //3*3, 3*5,3*7.....
        }
    }
    twin[1]=2;
    for(i=3;i<=n;i=i+2)
    {
        if(a[i]==0)
        {
           twin[k]=i;
           k++;
        }
    }



}

int main()
{
     long long int b,x,l,g,m,t=0,q;
     long double c,r,p;
     sieve();
     //freopen("10179in.txt","r",stdin);
     //freopen("10179out.txt","w",stdout);
     while(cin>>m)
     {
        t++;
        b=0;
        q=m;
        if(m==0)
        {
            break;
        }
        r=1.0;
        for(g=1;g<=k && twin[g]<=sqrt(m);g++)
        {
          c=0;
          p=twin[g];
          if(m%twin[g]==0)
          {

            while(m%twin[g]==0)
            {
                c++;
                m=m/twin[g];
                if(m==0 || m==1)
                {
                    break;
                }

            }

             r=r*(1-(1/p));


          }
        }

         if(m>1)
         {
             r=r*(1-(1/(double)m));
         }


         cout<<setprecision(10)<<q*(double)r<<endl;
     }

     return 0;
}



 


 


Share on Google Plus

About Ashadullah Shawon

I am Ashadullah Shawon. I am a Software Engineer. I studied Computer Science and Engineering (CSE) at RUET. I Like To Share Knowledge.
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment