/* Given two sequences {a_i} and {b_i} of positive integers, this
 * program computes a third sequence {c_i} such that c_i = gcd(a_i, b_i).
 * We use the C++ compiler to take advantage of the C++ command ("new")
 * for dynamic declaration of arrays.
 */

#include <stdio.h>

/* gcd() returns the greatest common divisor of two positive integers */

int gcd(int x, int y)
{
  int bigger, smaller, i, d;
  
  /* Determine which of the two arguments is the smaller (and thus the
   * largest possible gcd)
   */

  if (x > y)
  {
    bigger = x;
    smaller = y;
  }
  else
  {
    bigger = y;
    smaller = x;
  }
  
  /* Go through every integer up to the smaller of the two arguments; the 
   * largest number which divides both the given integers is the gcd
   */

  d = 1;
  for (i = 2; i <= smaller; i++)
  {
    if (((x%i) == 0) && ((y%i) == 0))
    {
      d = i;
    }
  }
  return(d);
}

int main()
{
  int N, i;
  int *a, *b, *c;
  
  /* Input */

  printf("Enter the number of terms in each sequence: ");
  scanf("%d", &N);
  if (N <= 0)
  {
    printf("ERROR: The sequence must have a positive number of terms\n");
    return(1);
  }
  
  a = new int[N];
  b = new int[N];
  c = new int[N];
  
  for (i = 0; i < N; i++)
  {
    printf("Enter a[%d]: ", i);
    scanf("%d", &a[i]);
    if (a[i] <= 0)
    {
      printf("ERROR: All terms must be positive\n");
      return(1);
    }
  }
  
  for (i = 0; i < N; i++)
  {
    printf("Enter b[%d]: ", i);
    scanf("%d", &b[i]);
    if (b[i] <= 0)
    {
      printf("ERROR: All terms must be positive\n");
      return(1);
    }
  }
  
  /* Compute the sequence {c_i} */

  for (i = 0; i < N; i++)
  {
    c[i] = gcd(a[i], b[i]);
  }
  
  /* Output */

  for(i = 0; i < N; i++)
  {
    printf("c[%d] = %d\n", i, c[i]);
  }
  
  return(0);
}

