“Sometimes it is the people no one can imagine anything of who do the things no one can imagine.” ― Sir Alan Turing


ADS

ADS

Sunday, April 9, 2017

UVa 11417 GCD Solution in C / C++



Source Code 

#include<stdio.h>

int main()

{

  int N;

  while (scanf("%d", & N) == 1)

  {

    if (N == 0)

      break;

    int G = 0, i, j;

    for (i = 1; i < N; i++)

    {

      for (j = i + 1; j <= N; j++)

      {

        G += GCD(i, j);

      }

    }

    printf("%dn", G);

  }

  return 0;

}

int GCD(int a, int b)

{

  if (b == 0)

    return a;

  return GCD(b, a % b);


}

No comments:

Post a Comment