Collatz conjecture: Difference between revisions

From Rosetta Code
Content added Content deleted
(new draft task w/ possibly-deleted content from Wikipedia)
 
No edit summary
Line 3: Line 3:
These programs calculate the [[wp:Collatz conjecture|Collatz sequence]] for a given integer, and stop if 1 is reached.
These programs calculate the [[wp:Collatz conjecture|Collatz sequence]] for a given integer, and stop if 1 is reached.


(These programs were copied here from the Wikipedia article on the Collatz conjecture, where they were less on-topic and some felt they should be removed. —2010-03-06)
These programs were written before this task was created; as such, the specific actual requirements of this task have not yet been determined.


=={{header|bash}}==
=={{header|bash}}==

Revision as of 19:15, 6 March 2010

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.

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>

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>

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

Java

<lang java>public class Collatz {

 public static void main(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>

Fortran 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>


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>

Python

<lang python>def collatz(n):

   while n > 1:
       print (n)
       if n%2==0:
           n = n/2
       else:
           n = (n*3)+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>