summaryrefslogtreecommitdiffstats
path: root/main.c
blob: 7e289a33de18c003e3a8ca1b2ec73264bdc79b74 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <stdio.h>
#include <stdlib.h>

void PrintHelp(void) {
    printf("Usage: collatz <number>"
           "\nMade by samuel@yakubos.org\n\n");
}

int main(int argc, char *argv[]) {
    if ( argc < 2 || argc > 2) {
        PrintHelp();
        return 1;
    }
    char *endptr;
    long int num;
    int  steps = 0;
    num = strtol(argv[1], &endptr, 10);

    printf("%li\n", num);
    while ( num > 1 ) {
        num = ( num & 1 ) ? num * 3 + 1 : num / 2;
        printf("%li\n", num);
        steps++;
    }

    printf("%s took %d steps\n", argv[1], steps);
    
}