#include <cs50.h>
int collatz(int n);
int main(void)
{
int n = get_int("Enter int: ");
int steps = collatz(n);
printf("%i\n", steps);
}
int collatz(int n)
{
if (n==1)
{
return 0;
}
else if ((n%2)==0)
{
return 1 + collatz(n/2);
}
else
{
return 1 + collatz(3 * n + 1);
}
}
I am geting stuck trying to visualise how the 'return 1' on each iteration of the function gets 'carried through'.
I can write the steps out on paper to show that it does work, however I am struggling to make clear in my mind without going through step-by-step, why you have to +1 on every iteration of collatz.