Collatz conjecture: Difference between revisions
(→{{header|Fortran 90}}: added 'works with', to assign to Fortran) |
(... but DO NOT add to this page as it is likely to go away.) |
||
Line 1: | Line 1: | ||
{{draft task}} |
{{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]]. ''' |
'''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. ''' |
||
These programs calculate the [[wp:Collatz conjecture|Collatz sequence]] for a given integer, and stop if 1 is reached. |
<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. |
These programs were written before this task was created; as such, the specific actual requirements of this task have not yet been determined. |
Revision as of 06:49, 3 April 2010
Note: There is an ongoing discussion on removing this draft task in favour of this one. Please give your opinion, but DO NOT add to this page as it is likely to go away.
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>
Befunge
<lang befunge>&>:.:1-|
>3*^ @ |%2: < v>2/>+</lang>
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>
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]
(println n) (if (> n 1) (if (even? n)
(recur (/ n 2)) (recur (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
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(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
<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>
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%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>