Collatz Conjecture

D

Dexter

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
 
A

Antoninus Twink

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;
}
 
L

Lew Pitcher

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. ------
 
S

santosh

Dexter said:
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.wolfram.com/CollatzProblem.html>
<http://en.wikipedia.org/wiki/Collatz_conjecture>
<http://www.numbertheory.org/php/collatz.html>

There is an ongoing thread over in comp.programming on this problem.
 
C

christian.bau

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,016
Latest member
TatianaCha

Latest Threads

Top