Problem Type : Number Theory
#include<stdio.h>
long int gcd(int a,int b)
{
long int temp;
while(b!=0)
{
temp = b;
b = a%b;
a = temp;
}
return a;
}
int main()
{
long int n,i,j,result;
while(scanf("%ld",&n)==1)
{
if (n==0)
break;
result=0;
for(i=1;i<n;i++)
{
for(j=i+1;j<=n;j++)
{
result+=gcd(i,j);
}
}
printf("%ld\n",result);
}
return 0;
}
#include<stdio.h>
long int gcd(int a,int b)
{
long int temp;
while(b!=0)
{
temp = b;
b = a%b;
a = temp;
}
return a;
}
int main()
{
long int n,i,j,result;
while(scanf("%ld",&n)==1)
{
if (n==0)
break;
result=0;
for(i=1;i<n;i++)
{
for(j=i+1;j<=n;j++)
{
result+=gcd(i,j);
}
}
printf("%ld\n",result);
}
return 0;
}
No comments:
Post a Comment