CSCI 102 - Fall 2023 Fundamentals of Computation

#include <iostream>
using namespace std;

int main()
{
  int a = 24, b = 9, x, y;

  /* We can think of this outer for loop as controlling our inputs. One way we often see
     nested loops used is to control input with the outer loop, and perform some sort
     of operation with the inside loop. This for loop, along with the operations ran on the inside
     allow us to run the internal code 3 times, changing the input to our inner loop */
  for(int i=2; i <= 4; i++){
    // Assign x and y to a and b's values
    x = a;
    y = b;

    /* While this section may seem ambiguous at first, it actually is the "algorithm" to compute
       the greatest common denominator (GCD) between x and y. One way we could determine this is by seeing
       what happens to our inputs when this code is ran. For example, if x = 24 and y = 9, the following
       happens as the code executes:
        1st Iteration: y = 6, x = 9
        2nd Iteration: y = 3, x = 6
        2nd Iteration: y = 0, x = 3
      This means that outside the loop, 3 will be printed out. This makes sense as the GCD between 24 and 9
      is 6. As we can see (and talked about earlier), the body of the for loop essentially calculates the GCD
      of a and b. */
    while( y != 0 ){
      int t = y;
      y = x % y;
      x = t;
    }
    //Print out x (the greatest common denominator of a and b)
    cout << x << endl;

    //Update a and b to new values, which are passed into x and y when we loop
    a += 3;
    b *= i;

  }
  return 0;
}

//The three values for a and b used throughout this program are:
    //1. a = 24, b = 9
    //2. a = 27, b = 18
    //3. a = 30, b = 54

/* Program Output
3
9
6
*/