Monday, March 16, 2015

11417 - GCD (UVa)

#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;

int gcd(int a, int b)
{
    if(b==0) return a;
    return gcd(b, a%b);
}

int main()
{
    int n, i, j;
    int G;
    while(scanf("%d", &n)==1)
    {
        if(n==0) break;
        G=0;
        for(i=1;i<n;i++)
        for(j=i+1;j<=n;j++)
        {
            G+=gcd(i,j);
        }
        printf("%d\n", G);
    }
}

No comments:

Post a Comment

Compare equality of two string in C

#include <stdio.h> #include<string.h> int main() {     char* country = "Bangladesh";     char* country2;     ...