Collatz conjecture: Difference between revisions

From Rosetta Code
Content added Content deleted
(... but DO NOT add to this page as it is likely to go away.)
(Remove draft task. Add #REDIRECT Hailstone sequence)
 
(9 intermediate revisions by 8 users not shown)
Line 1: Line 1:
#REDIRECT [[Hailstone sequence]]
{{draft task}}
'''Note: There is an ongoing discussion on removing this draft task in favour of [[Hofstadter-Conway $10,000 sequence|this one]]. Please [[Talk:Collatz conjecture#Retire this task?|give your opinion]], but DO NOT add to this page as it is likely to go away. '''

<br>These programs calculate the [[wp:Collatz conjecture|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.

=={{header|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>

=={{header|Befunge}}==
<lang befunge>&>:.:1-|
>3*^ @
|%2: <
v>2/>+</lang>

=={{header|C}}==
<lang c>#include <stdio.h>
#include <stdlib.h>

int collatz( int n )
{
printf( "%i\n", n );

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

int main( int argc, char** argv )
{
int n = atoi( argv[1] );
int i = collatz( n );
printf( "%i\n", i );

return 0;
}</lang>

=={{header|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>

=={{header|Clojure}}==
<lang lisp>(defn collatz [n]
(println n)
(if (> n 1) (if (even? n)
(recur (/ n 2))
(recur (inc (* 3 n))))))

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

=={{header|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>

=={{header|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

=={{header|Ioke}}==
<lang ioke>collatz = method(n,
n println
unless(n <= 1,
if(n even?, collatz(n / 2), collatz(n * 3 + 1)))
)</lang>

=={{header|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>

=={{header|Fortran}}==
{{works with|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>

=={{header|Haskell}}==
<lang haskell>collatz :: Integer -> Integer
collatz n
| n == 1 = 1
| even n = collatz (n `div` 2)
| otherwise = collatz (3 * n + 1)</lang>


=={{header|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>

=={{header|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>

=={{header|PHP}}==
<lang php>while($n > 1)
{
if($n % 2 != 0)
{
$n = $n*3 + 1;
}
else
{
$n = $n/2;
}
echo $n . '<br/>';
}</lang>

=={{header|PureBasic}}==
<lang PureBasic>While n>1
If n%2
n=(3*n)+1
Else
n/2
EndIf
PrintN(Str(n))
Wend</lang>

=={{header|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>

=={{header|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>

Latest revision as of 21:49, 13 November 2011

Redirect to: