Thursday, June 8, 2017

uva problem solution 11417 - GCD


problem link:

Discuss: in this problem how to find gcd then do the code as in the statement.


try yourself before see the code


#include<bits/stdc++.h>
using namespace std;
int gcd(int n1,int n2)
{
int g;
   for( int i=1; i <= n1 && i <= n2; ++i)
    {

        if(n1%i==0 && n2%i==0)
            g = i;
    }
   return g;

}
int main()
{
  long long n,i,j,sum;
  while(cin >>n)
  {
  sum=0;
     if(n==0)
     break;
     for(i=1;i<n;i++)
     {
       for(j=i+1;j<=n;j++)
       {
         sum+=gcd(i,j);

       }


     }
     cout <<sum<<endl;
  }
return 0;


}

No comments:

Post a Comment