source: trunk/Examples/Others/collatz.b

Last change on this file was 11, checked in by chronos, 12 years ago
  • Added: Some other examples from web.
File size: 1.6 KB
Line 
1>,[
2 [
3 ----------[
4 >>>[>>>>]+[[-]+<[->>>>++>>>>+[>>>>]++[->+<<<<<]]<<<]
5 ++++++[>------<-]>--[>>[->>>>]+>+[<<<<]>-],<
6 ]>
7 ]>>>++>+>>[
8 <<[>>>>[-]+++++++++<[>-<-]+++++++++>[-[<->-]+[<<<<]]<[>+<-]>]
9 >[>[>>>>]+[[-]<[+[->>>>]>+<]>[<+>[<<<<]]+<<<<]>>>[->>>>]+>+[<<<<]]
10 >[[>+>>[<<<<+>>>>-]>]<<<<[-]>[-<<<<]]>>>>>>>
11 ]>>+[[-]++++++>>>>]<<<<[[<++++++++>-]<.[-]<[-]<[-]<]<,
12]
13
14[The Collatz problem or 3n+1 problem is as follows. Take a natural number n.
15If it's even, halve it; if odd, triple it and add one. Repeat the process with
16the resulting number, and continue indefinitely. If n is 0, the resulting
17sequence is 0, 0, 0, 0... It is conjectured but not proven that for any
18positive integer n, the resulting sequence will end in 1, 4, 2, 1...
19See also http://www.research.att.com/projects/OEIS?Anum=A006577
20
21This program takes a series of decimal numbers, followed by linefeeds (10).
22The entire series is terminated by an EOF (0 or "no change"). For each number
23input, the program outputs, in decimal, the number of steps from that number
24to zero or one, when following the rule above. It's quite fast; on a Sun
25machine, it took three seconds for a random 640-digit number.
26
27One more note. This program was originally written for Tristan Parker's
28Brainfuck Texas Holdem contest, and won by default (it was the only entry);
29the version I submitted before the contest deadline is at
30http://www.hevanet.com/cristofd/brainfuck/oldcollatz.b
31
32Daniel B Cristofani (cristofdathevanetdotcom)
33http://www.hevanet.com/cristofd/brainfuck/]
Note: See TracBrowser for help on using the repository browser.