Home>
Contents of question

I am very sorry for the rudimentary question. I have a question about c language. I want to write a program that counts the number of times a function is called for the Collatz problem, but it doesn't work. As a condition, it is essential not to use loop functions such as while statements and for statements. I programmed it with a recursive function. The source code is below.

# Include
int h (int n) {
int count = 0;
if (n == 1) {
count ++;
return count;
} else {
if (n% 2 == 0) {
count ++;
n == n% 2;
h (n);
} else {
count ++;
n == 3 * n + 1;
h (n);
}
}
}
int main (void) {
int n = 2020, count;
count = h (n);
printf ("% d", count);
return 0;
}

result

The function itself works, but I don't see any results. What is the cause of this? If i know, please give me an answer.

c
  • Answer # 1

    First, the code is rather wrong.
    When you call h () inside h (), you are passing the exact same value of n as an argument, so it will recurse until it eats up the stack.
    n == n% 2;This is because the value of n is not updated. (Furthermore, it seems that n% 2 is wrong because of the problem to be dealt with)

    The return is written in the function h ()if (n == 1) {}Only in the block of.
    So it is a mystery what the return value will be when you pass a non-1 value from main to h.
    (What happens when a function that should return a return exits a path without a return? I don't know if there is a provision for that behavior, but it's not the desired process anyway. I think there will be.)

    In terms of what I want to do, I think the variable count should probably be a static variable.

    It looks like a mess as a whole.
    Try to print the value of the argument n with printf () or something every time the function h () is called, and check what the current behavior is (what is happening). I think it's a good idea to start with that.


    [Addition]
    It is a situation where {counting the number of times a function is called, the Collatz problem with recursion} and two things you want to do are mixed.
    If you are confused when you think about this at the same time, it may be easier to think about it if you solve it one by one.

    If you want to count the number of times a function is called, you can write it straightforwardly as follows.

    int h (int n)
    {
      static int count = 0;// Prepare a counter with static.
      ++ count;// Increment the counter when called.
      //(processing)
      return count;// return the current counter value
    }

    So, after that, the processing content of "How about recursion due to the Collatz problem" is described in the above code.//(processing)You can add it to the part of.
    If you googled "Collatz Conjecture" and wrote the content you saw on wikipedia as it is, wouldn't it be like this?

    if (n! = 1) // When not 1
    {
      if (n% 2 == 0) {h (n/2);} // Divide by 2 if it is a multiple of 2
      else {h (n * 3 + 1);} // If not, multiply by 3 and add 1
    }

  • Answer # 2

    #include<stdio.h>
    int h (int n, int c) {
      if (n == 1) {
        return c;
      };
      if (n% 2 == 0) {
        return h (n/2, c + 1);
      } else {
        return h (n * 3 + 1, c + 1);
      }
    }
    int main (void) {
      int n = 3;
      int count = h (n, 0);
      printf ("% d", count);
      return 0;
    }


    Depending on n, it will be a huge value, so it is not recommended to solve it recursively, but ...