Notes week of Oct 17

gcd.cpp

// Euclid's algorithm for GCD
#include <iostream>
using namespace std;
int main()
{
    long a = 0;
    long b = 0;
    cout << "Enter first integer: ";
    cin >> a;
    cout << "Enter second integer: ";
    cin >> b;
    // TODO: error-check, ints must be positive.
    while( a != b ) // Whether to KEEP GOING
    {
        if(a < b)
        {
            b = b - a;
        }
        else
        {
            a = a - b;
        }
    }
    cout << a << endl;
    return 0;
}

collatz.cpp

// Collatz sequence
#include <iostream>
using namespace std;
int main()
{
    unsigned int x = 1;
    cout << "Enter a positive integer: ";
    cin >> x;
    cout << x << endl;
    while( x != 1 )
    {
        if(x%2 == 0) // Is x even?
        {
            x = x / 2;
        }
        else
        {
            x = 3*x + 1;
        }
        cout << x << endl;
    }
    return 0;
}