formations/python-perfs/examples/my_collatz_length.c

14 lines
253 B
C

// Compile with:
// cc -c -fPIC my_collatz_length.c -o libcollatz.so
int collatz_length(long n)
{
if (n == 1)
return 0;
if (n % 2 == 0)
return 1 + collatz_length(n / 2);
else
return 1 + collatz_length(n * 3 + 1);
}