#include #include #include size_t collatz(size_t long_num) { return ( long_num & 1 ) ? ((long_num << 1) + long_num ) + 1 : long_num >> 1; } size_t printer(size_t long_num, bool verbose) { size_t steps = 0; /* Probably not the best way to do this */ switch (verbose) { case true: while ( long_num > 1 ) { long_num = collatz(long_num); printf("%ld\n", long_num); steps++; } break; case false: while ( long_num > 1 ) { long_num = collatz(long_num); steps++; } } return steps; } int print_help(char *argv[]) { fprintf(stderr,"Usage: %s [-v]" "\nMade by samuel@yakubos.org\n\n", argv[0]); return 1; } int main(int argc, char *argv[]) { if ( argc < 2 || argc > 3 ) return print_help(argv); bool verbose = false; char *endptr; long int_number = strtol(argv[1], &endptr, 10); /* check the number for nonsense */ if ( int_number <= 0 ) return print_help(argv); if ( *endptr != '\0' ) return print_help(argv); if (argc >= 3 && argv[2][1] == 'v') { verbose = true; } size_t long_num = int_number; printf("%s took %ld steps\n", argv[1], printer(long_num, verbose)); return 0; }