Collatz conjecture

From Rosetta Code
Revision as of 20:39, 22 September 2011 by rosettacode>Dkf (Make it clearer that this draft task is likely doomed)
Note: There is an ongoing discussion on removing this draft task in favour of Hailstone sequence. Please give your opinion, but DO NOT add to this page as it is likely to go away.
Collatz conjecture is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.

These programs calculate the Collatz sequence for a given integer, and stop if 1 is reached.

These programs were written before this task was created; as such, the specific actual requirements of this task have not yet been determined.

ALGOL 68

Translation of: C++

- note: This specimen retains the original Python coding style.

Works with: ALGOL 68 version Revision 1 - no extensions to language used
Works with: ALGOL 68G version Any - tested with release 1.18.0-9h.tiny
Works with: ELLA ALGOL 68 version Any (with appropriate job cards) - tested with release 1.8-8d

"as it is likely to go away" ... OOPS... translation was so easy I couldn't resist! :-) <lang algol68> main: (

 INT n; read(n);
 print((n,":"));
 FOR i WHILE n > 1 DO
   n:=IF ODD n THEN
     n*3 + 1
   ELSE
     n OVER 2
   FI;
   print(" "+whole(n,0))
 OD;
 print(new line)

)</lang> Input: 27

Output:

       +27: 82 41 124 62 31 94 47 142 71 214 107 322 161 484 242 121 364 182 91 274 137 412 206 103 310 155 466 233 700 350 175 526 263 790 395 1186 593 1780 890 445 1336 668 334 167 502 251 754 377 1132 566 283 850 425 1276 638 319 958 479 1438 719 2158 1079 3238 1619 4858 2429 7288 3644 1822 911 2734 1367 4102 2051 6154 3077 9232 4616 2308 1154 577 1732 866 433 1300 650 325 976 488 244 122 61 184 92 46 23 70 35 106 53 160 80 40 20 10 5 16 8 4 2 1

Befunge

<lang befunge>&>:.:1-|

 >3*^ @
 |%2: <
v>2/>+</lang>

C

<lang c>#include <stdio.h>

  1. include <stdlib.h>

int collatz(const int n) {

(void) printf("%i\n", n);
return (n == 1) ? 1 : ((n % 2) == 0) ? collatz( n / 2 ) : collatz(1 + (n * 3));    

}

int main(int argc, char* argv[]) {

int n = atoi(argv[1]);
int i = collatz(n);
(void) printf("%i\n", i);
return EXIT_SUCCESS;

}</lang>

C++

<lang cpp>#include <iostream> using namespace std;

int main() {

   int n;
   cin >> n;
   while (n > 1) {

if (n%2 != 0) { n = n*3 + 1; } else { n = n/2; } cout << n << endl;

   }

}</lang>

Clojure

<lang lisp>(defn collatz [n] (take-while identity (iterate #(cond (even? %) (/ % 2) (= % 1) nil :else (inc (* % 3))) n)))

(collatz (read-string (read-line)))</lang>

Common Lisp

<lang lisp>(defun collatz (n)

 (format t "~a~%" n)
 (cond ((= n 1) 1)
       ((= (mod n 2) 0) (collatz (/ n 2)))
       (t (collatz (+ 1 (* n 3))))))

(collatz (read))</lang>

Excel

   In cell A1, place the starting number.
   In cell A2 enter this formula =IF(A1/2=ROUND(A1/2,0),A1/2,A1*3+1)
   Drag and copy the formula down until 4, 2, 1

Fortran

Works with: Fortran version 90

<lang fortran>program main

 integer(4) :: n,i
 write(6,*) "Input a natural number: "
 read (6,*) n
 do while(n.gt.1)
    if(mod(n,2).eq.0) then ! number is even
       n=n/2
    else                   ! number is odd
       n=3*n +1
    endif
    write(6,*) n     
 enddo

end program main</lang>

Haskell

<lang haskell>collatz :: Integer -> Integer collatz n

   | n == 1    = 1
   | even n    = collatz (n `div` 2)
   | otherwise = collatz (3 * n + 1)</lang>


Ioke

<lang ioke>collatz = method(n,

 n println
 unless(n <= 1,
   if(n even?, collatz(n / 2), collatz(n * 3 + 1)))

)</lang>

Java

<lang java>public class Collatz {

public static void main(final String[] args)
{
 for (int x = Integer.parseInt(args[0]); x > 1; x = (x % 2 == 0) ? x / 2 : x * 3 + 1)
  System.out.println(x);
}

}</lang>

Oz

<lang oz>declare

 fun lazy {CollatzSeq N}
    N > 0 = true %% assert
    if N == 1 then         [1]
    elseif {IsEven N} then N|{CollatzSeq N div 2}
    else                   N|{CollatzSeq 3*N+1}
    end
 end

in

 {ForAll {CollatzSeq 42} Show}</lang>

Perl

<lang perl>#!/usr/bin/perl

die("Usage: $0 n\n") if (!$ARGV[0] || !int($ARGV[0]));

my $n = int($ARGV[0]);

while ($n > 1) {

 $n = ($n % 2 != 0) ? $n * 3 + 1 : $n / 2;
 print "$n\n";

} </lang>

PHP

<lang php>while($n > 1) {

  if($n % 2 != 0)
  {
     $n = $n*3 + 1;
  }
  else
  {
     $n = $n/2;
  }
  echo $n . '
';

}</lang>

PureBasic

<lang PureBasic>While n>1

 If n%2
   n=(3*n)+1
 Else
   n/2
 EndIf
 PrintN(Str(n))

Wend</lang>

Python

<lang python>def collatz(n):

   while n > 1:
       print (n)
       if n & 1:
           n = (n*3)+1
       else:
           n >> 1
   print (n)</lang>

Scala

<lang scala>def collatz(n:BigInt):Stream[BigInt] =

   if (n == 1) {
       Stream(1);
   } else {
       def next(n:BigInt):BigInt = if ((n % 2) == 0) (n / 2) else (n * 3 + 1);
       Stream.cons(n, collatz(next(n)));
   }</lang>

UNIX Shell

Works with: Bash

<lang sh>collatz () { n=$1; echo $n; if [ "$n" -gt 1 ]; then

  #if odd
  if [ "$(expr $n % 2)" -eq 1 ] ;then 
     collatz $(($n*3+1)) 
  else
     collatz $(($n/2))
  fi

fi }</lang>