Collatz Conjecture

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Dexter

    Collatz Conjecture

    In a programming book, I found this recursive algorithm which always
    returns 1 regardless of the value of the input parameter

    =============== ===

    int collatz (int n) {
    if (n == 1)
    return 1;
    else{
    if ( n%2 == 1)
    return collatz(3*n+1);
    else
    return collatz(n/2);
    }
    }

    =============== ===

    The author of the book asserts its unproven that it will indeed always
    return 1
  • Antoninus Twink

    #2
    Re: Collatz Conjecture

    On 26 Apr 2008 at 18:16, Dexter wrote:
    In a programming book, I found this recursive algorithm which always
    returns 1 regardless of the value of the input parameter
    Here's a *non*-recursive algorithm which always returns 1 regardless of
    the value of the input parameter:

    int f(int n)
    {
    return 1;
    }

    Comment

    • Lew Pitcher

      #3
      Re: Collatz Conjecture

      In comp.lang.c, Dexter wrote:
      In a programming book, I found this recursive algorithm which always
      returns 1 regardless of the value of the input parameter
      >
      =============== ===
      >
      int collatz (int n) {
      if (n == 1)
      return 1;
      else{
      if ( n%2 == 1)
      return collatz(3*n+1);
      else
      return collatz(n/2);
      }
      }
      >
      =============== ===
      >
      The author of the book asserts its unproven that it will indeed always
      return 1
      In at least one case, it will not return 1

      If n == 0 then it will never return at all.

      --
      Lew Pitcher

      Master Codewright & JOAT-in-training | Registered Linux User #112576
      http://pitcher.digitalfreehold.ca/ | GPG public key available by request
      ---------- Slackware - Because I know what I'm doing. ------


      Comment

      • santosh

        #4
        Re: Collatz Conjecture

        Dexter wrote:
        In a programming book, I found this recursive algorithm which always
        returns 1 regardless of the value of the input parameter
        >
        =============== ===
        >
        int collatz (int n) {
        if (n == 1)
        return 1;
        else{
        if ( n%2 == 1)
        return collatz(3*n+1);
        else
        return collatz(n/2);
        }
        }
        >
        =============== ===
        >
        The author of the book asserts its unproven that it will indeed always
        return 1
        See:

        <http://mathworld.wolfr am.com/CollatzProblem. html>
        <http://en.wikipedia.or g/wiki/Collatz_conject ure>
        <http://www.numbertheor y.org/php/collatz.html>

        There is an ongoing thread over in comp.programmin g on this problem.

        Comment

        • christian.bau

          #5
          Re: Collatz Conjecture

          On Apr 26, 7:16 pm, Dexter <yousaf.a...@gm ail.comwrote:
          In a programming book, I found this recursive algorithm which always
          returns 1 regardless of the value of the input parameter
          >
          =============== ===
          >
          int collatz (int n) {
            if (n == 1)
              return 1;
            else{
              if ( n%2 == 1)
                 return collatz(3*n+1);
              else
                 return collatz(n/2);
            }
          >
          }
          >
          =============== ===
          >
          The author of the book asserts its unproven that it will indeed always
          return 1
          If he or she makes that claim, then the author is completely wrong.

          On many implementations , calling this function will result in a stack
          overflow whenever the function argument is a negative number. It will
          also result in stack overflow on many implementations if the argument
          is an odd integer between INT_MAX / 3 and INT_MAX / 3 * 2.

          Comment

          Working...