#include <iostream>
#include <sstream>
#include <iomanip>
#include <string>
// Collatz conjecture visualizer
// © Copyright Robert White, 2019
using namespace std;
int main(int argc, char *argv[])
{
int retval = 0;
char fill = '_';
int shift = 0;
unsigned mode = 6;
unsigned long value = 1023;
auto format = [&](unsigned long v) -> string { string r(shift,fill); do {r += ((v&1)?'1':'0');} while (v >>= 1); return r; };
auto display = [&](unsigned m, char t, unsigned long v) -> void { if (m&mode) {cout << t << ':' << setw(12) << v << ": " << format(v) << endl; }};
if (argc > 1) { // first argument supplies value to process if present.
int temp;
std::stringstream argvalue;
argvalue << string(argv[1]);
argvalue >> value;
}
if (argc > 2) { // second argument is display mode
int temp;
std::stringstream argvalue;
argvalue << string(argv[2]);
argvalue >> mode;
if (mode == 0) mode = 6;
}
display(0xFF,'S',value);
do {
if (value&1) {
value *= 3;
display(2,'M',value);
value += 1;
display(4,'I',value);
// let the next pass do the divide by 2 for clarity of the shift term
} else {
value >>= 1; // fast divide by 2
shift++; // keep track of how many times we've divided
display(1,'C',value);
}
} while (value > 1);
display(0xFF,'F',value);
return retval;
}