[Question]
      [
Given a nonempty list of positive integers \$(x, y, z, \dots)\$, your job is to determine the number of unique values of \$\pm x \pm y \pm z \pm \dots\$
For example, consider the list \$(1, 2, 2)\$. There are eight possible ways to create sums:
* \$+ 1 + 2 + 2 \to +5\$
* \$+ 1 + 2 − 2 \to +1\$
* \$+ 1 − 2 + 2 \to +1\$
* \$+ 1 − 2 − 2 \to −3\$
* \$− 1 + 2 + 2 \to +3\$
* \$− 1 + 2 − 2 \to −1\$
* \$− 1 − 2 + 2 \to −1\$
* \$− 1 − 2 − 2 \to −5\$
There are six unique sums \$\{5, -5, 1, -1, 3, -3\}\$, so the answer is \$6\$.
## Test Cases
```
[1, 2] => 4
[1, 2, 2] => 6
[s]*n => n+1
[1, 2, 27] => 8
[1, 2, 3, 4, 5, 6, 7] => 29
[3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5] => 45
[1, 7, 2, 8, 3, 1, 6, 8, 10, 9] => 56
[93, 28, 92, 100, 43, 66, 2, 98, 2, 52, 57, 75, 39, 77, 45, 15, 13, 82, 81, 20, 68, 14, 5, 3, 72, 56, 57, 1, 23, 25, 76, 59, 60, 71, 71, 24, 1, 3, 72, 84, 72, 28, 83, 62, 66, 45, 21, 28, 49, 57, 70, 3, 44, 47, 1, 54, 53, 56, 36, 20, 99, 9, 89, 74, 1, 14, 68, 47, 99, 61, 46, 26, 69, 21, 20, 82, 23, 39, 50, 58, 24, 22, 48, 32, 30, 11, 11, 48, 90, 44, 47, 90, 61, 86, 72, 20, 56, 6, 55, 59] => 4728
```
[Reference solution](https://tio.run/##lVJNT4NAFLzzK16MhzZdDR/LshjryYuJiQePhAOmaBtxaYQqB/97nXFRE6MNEh67@/bNm5kX1lX3WDfNfr952rbPvVxWfXV6vel6mbnd3TwI6mFbuZWcnUlx5fpSTi4EK5eP8@f90MkgSwFGjmXVipPzEyQD@f0pnCxkUChDVRkEXdu81D9JxuxSmto99Gs5lfu2WTUyUhYhgE/VxhF3dSOzuT8tZbvrb/tnCNm5ZuPqDruiW7evkLhYyJEsL/DB7iM38yzD/C@tfN4AhaGiiJTEpTpU@mWRpf@q/n7/xZAo0UpSJUZJNhGZAxRbJXmsojBEA5wN8OyXW7@mjAw90TrJsWKvsY8YqLe4t5QQTuIUg74RlFIqRWfsbzwHXcfUhLuMOfAZ6MoiH7H2NZ84q6dxspY@Lf3F3iM9xJHP63z0GPreGjx61JNyqonXmJjpPnP0FITlzEbd9E3/7M17g5xmT4TJRz2hnynnwHmnOKd2GifnEwOrwZHwnwA2inwwl4ff3rgnvzV@luSlR/4/acrZl@UB0nK/fwc) (optimizes for speed and not size).
If you can't handle the last case because you use a brute-force method or similar, that's fine.
## Scoring
This is [code-golf](/questions/tagged/code-golf "show questions tagged 'code-golf'"), so the shortest valid solution (measured in bytes) wins.

[Answer]
# [Wolfram Language (Mathematica)](https://www.wolfram.com/wolframscript/), 27 bytes
```
Tr[1^Fold[#⋃+##&,{0},#]]&
```
[Try it online!](https://tio.run/##bVLBSsQwEL33KwqBohgxSZNJgih78uzBW6lQVhcXXIWlt9KTR//SH6nzMpVehJ3N9M17My/Tnobx7fU0jMf9sBzuLpanc2efHz7fXzr18/11pVSjJzNr1ffNcnlbPZ6PH2Onru8PO9U3N7upqqrJ6trNWs4t/ee3kVpde10HXZOuI@C2MHz5ZzgXFpWcS2FVxgKngtlS59wa5oOQGXUMZAeQUc8AUdHkVI6A4C4RfXlI5NxzbhHMTmgPi6wm9PZ/FiKUJGoQMIsrERA3IhZEK@HkGqsoeTnhLMGQE1MY66zgPq@2jKyGJV7mBBhoZXRLYiznsqAE/zIKNmEXIlQJuwSbg/I6x8j14BxXD/wckrh1jHusFa@GcWslgGWzGUKO3onWOxlxhiUELGLm72FefgE "Wolfram Language (Mathematica) – Try It Online")
Finding the number of unique sign-swapping sums is equivalent to finding the number of unique subset sums.
A proof would involve adding the sum of the input to each of the sign-swapping sums and dividing by two. Then, there's an obvious bijection.
### Explanation
```
Fold[#⋃+##&,{0},#]
```
Iterate through the input, with the initial value being `{0}`: take the union between `<current value>` and `<current value> + input element` (maps onto lists).
```
Tr[1^ ... ]
```
Golfy version of the `Length` function.
[Answer]
# [Jelly](https://github.com/DennisMitchell/jelly), 6 bytes
```
ŒPS€QL
```
[Try it online!](https://tio.run/##y0rNyan8///opIDgR01rAn3@H24H0pH//0dHG@ooGMXqcCmAGahsYx0FEx0FUx0FMx0Fc7A4UMQQLGgIFrcEKzMDs4FSpjC95mBxC7CgIVgBkG1oANQQyxULAA "Jelly – Try It Online")
### Background
Let **L** be the input list and **{P, N}** a partition into algebraic summands with positive and negative signs. The challenge spec requires calculating **s{P,N} = sum(P) - sum(N)**.
However, since **sum(P) + sum(N) = sum(L)** and **sum(L)** does not depend on the partition, we have **s{P,N} = sum(P) - sum(N) = sum(P) - (sum(L) - sum(P)) = 2sum(P) - sum(L)**.
Thus, each unique value of **sum(P)** corresponds to one unique value of **s{P,N}**.
### How it works
```
ŒPS€QL  Main link. Argument: A (array)
ŒP      Powerset; generate all subarrays of A.
  S€    Take the sum of each.
    Q   Unique; deduplicate the sums.
     L  Take the length.
```
[Answer]
# [MATL](https://github.com/lmendo/MATL), ~~11~~ 10 bytes
```
nW:qBGY*un
```
[Try it online!](https://tio.run/##y00syfn/Py/cqtDJPVKrNO///2hDayNro1gA "MATL – Try It Online")
This is a port of Luis Mendo's [Octave/MATLAB](https://codegolf.stackexchange.com/a/164527/31516) answer. I'm still trying to learn MATL, and I figured I'd post it, along with an explanation, since MATL is the language of the month.
### Explanation:
Here's a read-through for anyone unfamiliar with stack based programming in general, and MATL in particular.
The input vector is implicitly placed on the stack. Note that when an operation is performed on an element in the stack, then that element is removed from the stack.
```
                % Stack:
                % [1, 2, 2]
n               % Counts the number of elements of the vector on the top of the stack.
                % Stack:
                % [3]
 W              % Raise 2^x, where x is the number above it in the stack
                % Stack:
                % [8]
  :             % Range from 1...x, where x is the number above it in the stack.                    % Stack:
                % [1, 2, 3, 4, 5, 6, 7, 8]
   q            % Decrement. Stack:
                % [0, 1, 2, 3, 4, 5, 6, 7]
    B           % Convert to binary. Stack:
                % [0,0,0; 0,0,1; 0,1,0; 0,1,1; 1,0,0; 1,0,1; 1,1,0; 1,1,1]
     G          % Push input again. Stack:
                % [0,0,0; 0,0,1; 0,1,0; 0,1,1; 1,0,0; 1,0,1; 1,1,0; 1,1,1], [1; 2; 2]
      Y*        % Matrix multiplication of the two elements on the stack.
                % Stack:
                % [0; 2; 2; 4; 1; 3; 3; 5]
        u       % Keep only unique elements. Stack:
                % [0; 2; 4; 1; 3; 5]
         n      % Number of elements in the vector. Stack:
                % [6]
```
And then it outputs the final element on the stack implicitly.
[Answer]
# [Python 2](https://docs.python.org/2/), 55 bytes
```
s={0}
for n in input():s|={t+n for t in s}
print len(s)
```
[Try it online!](https://tio.run/##TVHBbsIwDD3Tr/CtoPXQhDRNKnFk1112mzhsLBNoqK3SooEY3975xZ02qVbsZz/72e2v46Fr9bTv3gNtKM/zadjcynv20UVq6YivP4/LVTN8b27jQ0tIjEgM96yPx3akU2iXw2pibpZ9HY6nQM/xHJpsMcZrQ@ES9oT22SJc9qEfafv0uI2xiw29xfD6Ob2ogvQuS89/b12QKagqyBZUM8qxSpBKqE9FNvmcqoRXJ9QlSKU0@6rkcs57BjXHXgNj0DBgbaJ4l54Kxk1qdOURNfuGfQXjaofu0Mdsi9bmV0ANphU2CjCLMzUgbmSZUCsxLUvMJGfkhTIHQVpEYaxWghs/yyrlLkwxMqeCgLWMXlsR5n06j4N@GQWZkAsSshaXRDWb9fOcUtaDcqxecVw5UasZN7gq/gvjSokB8@WfIPjo7ey8UynKcIQKh9j9AA "Python 2 – Try It Online")
[Answer]
# [Python 2](https://docs.python.org/2/), 52 bytes
```
k=1
for n in input():k|=k<<n
print bin(k).count('1')
```
[Try it online!](https://tio.run/##TVHBasMwDD03X6FbWggjdh3HDu2xu@6y2@hhyzwaMpJgUtbC/j3TszI2iLD0pCc9KdN9voyDXtrxPdCR8jxf@qPKPsZIA3X4puu83TX997E/HIZsit0w01s3bPvdQzteh3mbq3y3MDHLvi7dZ6DneA1NtpnjvaFwCy2hd7YJtzZMM52eHk8xjrGhtxhe@@VFFaTPWXr@e/uCTEFVQbagmlGOVYJUQn0qssnnVCW8OqEuQSql2Vcll3PeM6g59hoYg4YBaxPFu/RUMG5SoyuPqNk37CsYVzt0hz5mW7Q2vwJqMK2wUYBZnKkBcSPLhFqJaVliJTkjL5Q5CNIiCmO1Etz4VVYpd2GKkTkVBOxl9N6KMO/TeRz0yyjIhFyQkLW4JKrZrF/nlLIelGP1iuPKiVrNuMFV8V8YV0oMmC//BMFHb2fXnUpRhiNUOMT5Bw "Python 2 – Try It Online")
Uses the binary representation of a number to store the reachable subset sums.
[Answer]
# [05AB1E](https://github.com/Adriandmen/05AB1E), 4 bytes
```
æOÙg
```
Utilizes the same approach used in [Dennis’ Jelly answer](https://codegolf.stackexchange.com/a/164512/59487).
[Try it online!](https://tio.run/##MzBNTDJM/f//8DL/wzPT//@PNtQx0jGKBQA "05AB1E – Try It Online")
[Answer]
## Haskell, 46 bytes
```
import Data.List
length.nub.map sum.mapM(:[0])
```
[Try it online!](https://tio.run/##TYrRCsIgGEbv9xT/5QY/MrO5Fuyuy3oCkTDYmkxNpnt@Uyro6hzO9y0qrJMxKWnrX1uEi4qKXHWI1TyayT3jQtz@IFZ5CLstvNVn0comWaUdjFDKHWq/aReBwNyAqAAExYPEr/wrwyN2yLH/JIY0B5rTkEeeybD7vfucTlguPJO2OEgJ6Q0 "Haskell – Try It Online")
Instead of summing the subsets of the input list, we make all combinations of either keeping a number or replacing it by `0`, e.g.
```
mapM(:[0])[1,2,3] -> [[1,2,3],[1,2,0],[1,0,3],[1,0,0],[0,2,3],[0,2,0],[0,0,3],[0,0,0]]
```
This is two bytes shorter than `subsequences`.
[Answer]
# R, ~~83~~ 75 bytes
### -8 bytes thanks to JayCe and Giuseppe
Creates a matrix of all possible combinations of (1,-1) for the size of the input vector, multiples this by the original vector to get the sums. Then unique and find the length of the result.
`function(v)nrow(unique(t(t(expand.grid(rep(list(c(1,-1)),sum(v|1)))))%*%v))`
---
### previous version:
`f=function(v)nrow(unique(as.matrix(expand.grid(rep(list(c(1,-1)),length(v))))%*%v))`
### Ungolfed with comments:
```
f=function(vector){
  List=rep(list(c(1,-1)),length(vector))   ## Create a list with length(vector) elements, all (1,-1)
  Combinations=expand.grid(Length)    ## get all combinations of the elements of the list, e.g, 1,-1,1,1,-1,1
  Matrix=as.matrix(Combinations)   ## convert to matrix
  Results=Matrix%*%vector   ## multiply the matrix original vector to get a Nx1 matrix
  Unique_results=unique(Results)   ## unique the results
  nrow(Unique_results)   ## length = number of rows of unique values
  }
```
[Answer]
# [Octave](https://www.gnu.org/software/octave/) / MATLAB, ~~45~~ ~~41~~ 40 bytes
```
@(x)nnz(unique(dec2bin(0:2^nnz(x)-1)*x))
```
Input is a column vector (using `;` as separator).
The code errors for the last test case due to memory restrictions.
This uses an idea from [JungHwan Min's answer](https://codegolf.stackexchange.com/a/164513/36398) (subsets instead of alternating signs) to save a few bytes.
[**Try it online!**](https://tio.run/##y08uSSxL/Z9mq6en999Bo0IzL69KozQvs7A0VSMlNdkoKTNPw8DKKA4kXKGpa6ipVaGp@T9NI9rQWsEoVpMLyDK1VoAiCB8kA5eEcIytFUzASsysFcwhEkAhQ7CoIVjCEqzODMw2RjbKHCxhARY1BKsAsg0NgDpiNf8DAA "Octave – Try It Online")
[Answer]
# [Pari/GP](http://pari.math.u-bordeaux.fr/), 39 bytes
```
a->#[1|n<-Vec(prod(i=1,#a,1+x^a[i])),n]
```
Counts the number of nonzero coefficients of the polynomial \$ \prod\_{i \in a}(1+x^i) \$, where \$a\$ is the input.
[Try it online!](https://tio.run/##RZHdSsQwEIVfJezetJhCJ80vuPsY3pQKQV0pSA3FCwXfvc7JRBYaMvkyZ@ZkWvK@Du/luKnLkYfreabf7XF4envpyv752q0X0ues6eH7Oc/r0vd6W45cysdPl9VwVWVfty8OTzic1K3LnKLmmbQyCwfYW@i1wnenk1ZWK1dxAGZAlVHFqWb5GvOVa8pQcayM6j3HNHI@EhJTwyAZQKaWgfdVk2LdHBZXCajLTQLHlmPC4uyI8rDIao/a9t9CgNKLGgnoxTcBiAt5FgSSZeQZTRSt7HAWYciIKbQ1JNymZmuU0bDESh8HA5O0nrwYS6kOKMK/tIJN2IUItx6zRDamnlqfUZ4H53i647OL4tYwtxgrfg1zIllgabwbQoza0bc3jeIMQ3AYxLL0xx8 "Pari/GP – Try It Online")
[Answer]
# [Python 3](https://docs.python.org/3/), 61 bytes
```
f=lambda a,s={0}:a and f(a[1:],s|{u+a[0]for u in s})or len(s)
```
[Try it online!](https://tio.run/##VVLLboMwELzzFXsEdQ82fmBH4ksQB9oEFSmFKCRSqjTfTmcx6UOw2vXsjHcMPn1e3qfRLEtfH7uP131HHc/1XT12qMY99XnX6F3L89f9@tI1qu2nM11pGGl@FCiPhzGfi@Wtmw8z1dRkRHmjuWyZbMHP1br229ryvwed@IdpgDn2XAEvnw3DGrBGI4LikQ07GeF@lBUagYXokbXiiL57zowGuwVMKpm0UlAC8B4gsLAmJ1ExVY7JRORK9gddAuyAftCgQu0h0RZ8UMEUpU9qIcgsdCqBsJGHoNIpSrtSNlGwKYuzIIbKZErGljrhNm621CqzkNg0x4kBk0Ybn4xFsPEG8Z9GiU2xKyLpemBW2AgftzkqHU@cy9Ed1i4ktyVwi9ogG@BapxAsql9DUsvewW9nUsmZfAQnH0J@V1WGImuzTC7RwDTJPVqvzg5/6Vb3@VCgOJ2H8ZLfmG5U1zQVyzc "Python 3 – Try It Online")
Recursive approach, keeping track of unique subset sums.
The real fun is that this beats `itertools` by a big margin:
## 76 bytes
```
lambda a:len({*map(sum,product(*([0,x]for x in a)))})
from itertools import*
```
[Try it online!](https://tio.run/##VY/RCsIwDEXf@xV5bEcenHPVDfYlcw9VNyysS@kqKOK3z1RUlEDCveeGNv4WzzQVy9Dsl9G4w8mAqcd@kvfMGS/ni0Mf6HQ5RpnJdoXXbqAAV7ATGKXUQ4khkAMb@xCJxhms8xRithzN3M/QQCsAZJvjukPYKPyol9ZvvcG/YlL9JAv2StS4ZX/9AQXmbOcMKo5ongWW6Ynyu7llsMMU1DzzFVbMS61EJ0Q6wiJQuuP105qXfLBTlIO0CiF1aBogtTwB "Python 3 – Try It Online")
[Answer]
# [Pyth](https://pyth.readthedocs.io), 5 bytes
```
l{sMy
```
Utilizes the same approach used in [Dennis’ Jelly answer](https://codegolf.stackexchange.com/a/164512/59487).
[Try it here.](http://pyth.herokuapp.com/?code=l%7BsMy&input=%5B1%2C+2%2C+3%2C+4%2C+5%2C+6%2C+7%5D&debug=0)
[Answer]
# [Attache](https://github.com/ConorOBrien-Foxx/Attache), 29 bytes
```
{#Unique[Flat!±_]}@Fold[`±]
```
[Try it online!](https://tio.run/##SywpSUzOSP2fpmBl@79aOTQvs7A0NdotJ7FE8dDG@NhaB7f8nJTohEMbY//7paamFEerFBQkp8dyBRRl5pWEpBaXOCcWpxZHp@koRHMpAEG0oY6CUawOgo3ENdFRQCBUNcZgQVMdBTMdBXOYFFDQECxuCJayBKs0A7OBUqZIJpiDpSzA4oZgNUC2oQFQTyxXbOx/AA "Attache – Try It Online")
This works by folding the `±` operator over the input list, then taking `±` of that list, then counting the unique atoms of the array.
Here's some examples of how the folding works:
```
Fold[`±][ [1,2] ] == 1 ± 2
                  == [1 + 2, 1 - 2]
                  == [3, -1]
Fold[`±][ [1,2,2] ] == (1 ± 2) ± 2
                    == [3, -1] ± 2
                    == [[3 + 2, 3 - 2], [-1 + 2, -1 - 2]]
                    == [[5, 1], [1, -3]]
                    == [5, 1, 1, -3]
Fold[`±][ [4,4,4,4] ] == (4 ± 4) ± 4 ± 4
                      == [8, 0] ± 4 ± 4
                      == [[12, 4], [4, -4]] ± 4
                      == [[[16, 8], [8, 0]], [[8, 0], [0, -8]]]
                      == [16, 8, 8, 0, 8, 0, 0, -8]
                      == [16, 8, 0, -8]
```
Then we generate all permutations of the final sign by applying PlusMinus once more.
## A more efficient version, 31 bytes
```
`#@(q:=Unique@Flat@`±)@Fold[q]
```
[Try it online!](https://tio.run/##RVDLasMwELz7KwTtoQUdLOthKRAwFHIsPbQnY4hJlDYQ0rh2P6y/0B9zZ7yGgpddj2Y0s@qnqT985PmkNtt5f9c8DJvt2/U8fOdmd@mnZv/789jsPi/Hdujm55yPY3t/ux3eu@Ll63ydXvM4PfVjHtuTVm3RJqtVFbVKlVamLLVyAEIACCwuzbNqrWqvlU3omB1mwwI74jwaUKEOkBgHPqhgUhlETQK9cFITwkUBgtpIVW6hrKLopDNZZKBKQtG2MoK7tMYqF5mDxImPZwAr1jZIsAQ2vsj8YsWYjEsRTwMwRzYqpNWnlPWYnKt7/PsoaSvgDrNFt8CNkSKWyv9AnHl3DOtOpSTjI3g@RFd03fwH "Attache – Try It Online") This doesn't time out on the final test case, since it doesn't generate unnecessary combinations.
[Answer]
# [R](https://www.r-project.org/), 62 bytes
```
function(V)sum(unique(c(V%*%combn(rep(0:1,L),L<-sum(V|1))))|1)
```
[Try it online!](https://tio.run/##K/qfpmCj@z@tNC@5JDM/TyNMs7g0V6M0L7OwNFUjWSNMVUs1OT83KU@jKLVAw8DKUMdHU8fHRhekKKzGUBMIgOT/NKBSQx0jHSNNzf8A "R – Try It Online")
Ports Dennis' algorithm. It's closest to the Octave/MATL answers as it uses a similar bitmap and matrix product for inclusion/exclusion of terms.
[Answer]
# [APL (Dyalog Classic)](https://www.dyalog.com/), ~~12~~ 11 bytes
-1 thanks to H.PWiz
```
≢⊃(+∪⊢)/⎕,0
```
[Try it online!](https://tio.run/##LVDLTgJBELzvV/RtIEKcmZ2nf7OBQIhEzLIXQzhpENAlJsajBzl61guJP9M/gtWrSc/2THdtVXVXt/Ph@K6aL6bD0bxaLmejMx/eZgvevOiCt4@TM@@OvL/vXfD2k/fH/iXaA31G6zwFiNuD4ecftV6pgUJLTq24/Va83RvevOI9qtVEEbfv1NTVeHJDzYKQigb/rwDl3UeN65Tb05VaXCt@elCTajYHzQntel0YstSQ67LcQtEL3H6lfkOxK5bkyFOgiKbNRUkGBYNSRjMglzhg8EBHlBIJJCAbDUxDPhQVPOSSbKJsUdbkSgoB4Jzw8YhIEUyZYgQTGURJCWRwoCmAy3VKEVBoxqIagBJNkHqKKGUKmqKRsOKvwyYnX8gm6FmRBLk1UnG5E9UynyMXyfxxegiVIlIGkc4ZMyT4Ek6YgBVgUQ1YAxCBQu4YtdiFHczgNfkkLqz943TYCRapyRgJPLP@V8UFTCl0PrXoYhaPcYpKlhpt@gU "APL (Dyalog Classic) – Try It Online")
[Answer]
# [Perl 5](https://www.perl.org/) `-p`, 39 bytes
```
s/^| /{+,-}/g;$_=grep!$s{eval$_}++,glob
```
[Try it online!](https://tio.run/##K0gtyjH9/79YP65GQb9aW0e3Vj/dWiXeNr0otUBRpbg6tSwxRyW@VltbJz0nP@n/f0MFIwVjBRMFUwUzBfN/@QUlmfl5xf91fU31DAwN/usWAAA "Perl 5 – Try It Online")
[Answer]
# JavaScript (ES6), 63 bytes
```
f=([c,...a],s=n=!(o={}))=>c?f(a,s-c)|f(a,s+c)&&n:o[s]=o[s]||++n
```
[Try it online!](https://tio.run/##bY5BCsIwEEX3niJuSkKnqalNaoXoQUoXJVpRSiJG3FjPHuNQEaSb4cN7f2Yu3aPz5na@3jPrDscQek0bA5zzrgWvrV5Sp58vxvTO7Hvagc8MGzGkhiWJ3brGt/ozxjFNbTDOejcc@eBOtKeNAFK0jJE8J@Vihv2wmsdrICUQCUQBqSa1qP/daAkUBbo1VhXmiOT3Azlzo0J3g6LAUsxiFZdMLanCGw "JavaScript (Node.js) – Try It Online")
[Answer]
# [Husk](https://github.com/barbuz/Husk), 5 bytes
```
LumΣṖ
```
[Try it online!](https://tio.run/##yygtzv6fq@GlpGBrp6B0aHlqsUbxo6ZGzdyics1D2/77lOaeW/xw57T///8bKhhxATGUNFYwUTBVMFMw5zJWMASyDYE8S6C4GZA2VjAFqjEH8iwUQLJmQNrQQMGSCwA "Husk – Try It Online")
Port of [Dennis' Jelly answer.](https://codegolf.stackexchange.com/a/164512/59487)
### Explanation
```
LumΣṖ                     [1,2,2]
    Ṗ  Powerset           [[],[1],[2],[1,2],[2],[1,2],[2,2],[1,2,2]]
  mΣ   Sum each           [0,1,2,3,2,3,4,5]
 u     Remove duplicates  [0,1,2,3,4,5]
L      Length             6
```
[Answer]
# [Perl 6](https://github.com/nxadm/rakudo-pkg), 33 bytes
```
{+set ([X](^2 xx$_)XZ*$_,)>>.sum}
```
[Try it online!](https://tio.run/##K0gtyjH7n1upoJamYPu/Wrs4tURBIzoiViPOSKGiQiVeMyJKSyVeR9POTq@4NLf2f3FipUKaBlBcIS2/iCvaUEfBKFYHQqMwjXUUTHQUTHUUzHQUzEHCQAFDsJghWNgSrMoMzAZKmUJ1moOFLcBihmB5INvQAKg@1vo/AA "Perl 6 – Try It Online")
[Answer]
# [Bash](https://www.gnu.org/software/bash/) + GNU utilities, 49 bytes
```
eval echo "{,-}${@//,/{+,-\}}\;"|bc|sort -u|wc -l
```
[Try it online!](https://tio.run/##TUvNCoJAEL77FMNip2axzdJEii7dOkS3wMu6TiiIhmt5UJ/dpj9oYOb7nVTbfOryoiRoSGcxZLUDPC3Z1mhLW@GeD6fjRbzdW1NU7RXEzILcgQDh/nqf3FILUv7bEz10CWTyGkSPcnT7veeh189RJuOYxGJIzWDrhv/uQ2dAllNWVzQpXDq83@vjCtcYYOj4qJgrVhH7AaOPa@6ErDb4SgNGtcDIeQI "Bash – Try It Online")
Input given as a comma-separated list at the command-line.
### Explanation
```
               ${@//,/{+,-\}}                      # Replace commas with {+,-}
          "{,-}${@//,/{+,-\}}\;"                   # Build a brace expansion with {+,-} before every number and ; at the end
eval echo "{,-}${@//,/{+,-\}}\;"                   # Expand to give every combination expression, separated by ;
                                |bc                # Arithmetically evaluate each line
                                   |sort -u        # Remove duplicates
                                            wc -l  # Count
```
[Answer]
# x86\_64 machine language for Linux, ~~31~~ 29 bytes
```
 0:   31 d2                   xor    %edx,%edx
 2:   6a 01                   pushq  $0x1
 4:   58                      pop    %rax
 5:   8b 0c 97                mov    (%rdi,%rdx,4),%ecx
 8:   48 89 c3                mov    %rax,%rbx
 b:   ff c2                   inc    %edx
 d:   48 d3 e3                shl    %cl,%rbx
10:   48 09 d8                or     %rbx,%rax
13:   39 d6                   cmp    %ese,%edx
15:   7c ee                   jle    5 <+0x5>
17:   f3 48 0f b8 c0          popcnt %rax,%rax
1c:   c3                      retq
```
[Try it online!](https://tio.run/##jZDdioMwEIWvt08xCIWkpGBM/UP6JMYLjcbtxbqLtRAQn92dxN2LpaypBGcmZ@Z8jurcK7Wu6nO4T6De6xH6sroG0gguTRtJk9TShJjHmTRZg7mSJk@ludg6l0YJabTGGG13Ldad2PIQ9RajwKhRT3G26zD/1XGuwahC6xMUh9swgSb4PtXM5g2dx256jAOxl@REnWYlSntKatbQYlk/6ttA6Hx4@xpR0STQpDzeKwpXOLZyCBgEnEEEOw/2aMcoKzqDrxsWBhBRWviQez7PyF2qRQoPMmdgz4tIX/dLyO27BYMLg5hBwiD1bvl/t0OmHiSOc@fAnUnuPNHKFqjFf5G@bovk3L9l6gYz9mOYuJyH2w982nK32yFDi1zWbw "C (gcc) – Try It Online")
Inspired by @xnor's answer. Requires a machine with the `POPCNT` instruction.
[Answer]
# [C (gcc)](https://gcc.gnu.org/), ~~74~~ 69 bytes
```
f(a,b)int*a;{long k=1;for(;b--;k|=k<<a[b]);a=__builtin_popcountl(k);}
```
[Try it online!](https://tio.run/##jZDfCoIwFIev6ykOQrDFEVr/TJZPYiLTWIg2pfTKfHbbVjcRucYudtjvfN/Ocv@S5@MoicCMFqpdCt5XtbpAGTEu6xvhme/z8hGVx6OIs4RyEaVp1hVVW6i0qZu87lRbkZLyYbyKQhHaz2fNTaMk8SSJF/eEQgSL80l5CB5DWMPE0hlJiO6OE9qDKw0DAqwp5S7lFOdbOWk1yo1DGSKY/afSlf5L@Xr3BmGLsEPYIwTOKX@nrTJwKHU7swRmIaFlapQp9N3uU@lKGyVj7ikD23jAN3Bvz2z1@sCvKSfTVrkyymF8Ag "C (gcc) – Try It Online")
C port of @xnor's answer
[Answer]
# [APL (Dyalog Classic)](https://www.dyalog.com/), ~~34~~ ~~33~~ 32 bytes
```
{≢∪⍵+.×⍨↑{,⍵∘.,U}⍣(≢1↓⍵)⊢U←¯1 1}
```
[Try it online!](https://tio.run/##SyzI0U2pTMzJT9dNzkksLs5M/v@/@lHnokcdqx71btXWOzz9Ue@KR20Tq3WA3EcdM/R0Qmsf9S7WACoxfNQ2GSio@ahrUeijtgmH1hsqGNb@dwMyKTGA61HfVCDHTcFQwUjBCMHTUTDSUTDWUTDRUTDVUTDTUTD/DwA "APL (Dyalog Classic) – Try It Online")
Thanks to @ngn for -1 byte!
[Answer]
# [Clean](https://clean.cs.ru.nl), 82 bytes
```
import StdEnv
f[]=length
f[h:t]=f t o foldr(\e l=removeDup[e+h,e-h:l])[]
?l=f l[0]
```
[Try it online!](https://tio.run/##RYw9C8IwFAD3/ooHLooRWgW/ILjUQXBzjBlCmpjAS1LaV8E/b4wubndwnEajYg6pm9BAUD5mH/o0ENyoO8dnZYXkaOKDXEF3JMktECSwCbthfjeAfDAhPU079cIsHTMrd0S5ELI6YWlR1DLfSA1UzQD9SMBBNAx2DNYM9gw2DIpuf9zUDA6yhHaKmnyKJT5V/K/fQX5ri@ox5tXlmttXVMHr8QM "Clean – Try It Online")
Defines the function `? :: [Int] -> Int` using `f :: [Int] -> ([Int] -> Int)` as a helper to reduce over each possible sum after an addition or subtraction.
[Answer]
# [APL (Dyalog Classic)](https://www.dyalog.com/), 21 bytes
```
⍴∘∪⊢+.×1-2×2(⍴⍨⊤∘⍳*)⍴
```
[Try it online!](https://tio.run/##SyzI0U2pTMzJT9dNzkksLs5M/v8/7VHbhEe9Wx51zHjUsepR1yJtvcPTDXWNDk830gAJ96541LUEJNm7WUsTKPD/Ud9UoI40IMWFlfnfUMFIwYgLRBormCiYKpgpmAN55kC@BVDEEMi3UDA0ULAEAA "APL (Dyalog Classic) – Try It Online")
## Explanation
```
2(⍴⍨⊤∘⍳*)⍴
```
A function train equivalent to `{((⍴⍵)⍴2)⊤⍳(⍴⍵)}`, which generates a matrix that has the binary representations of 0 to the length of the input as columns
```
1-2×
```
Maps `1`s to `-1`s and `0`s to `1`s
```
⊢+.×
```
Matrix multiplication with the input, which gives an array of all possible sums
```
⍴∘∪
```
Remove duplicates, then count
[Answer]
# Java 8, ~~207~~ ~~83~~ 44 bytes
```
s->Long.bitCount(s.reduce(1L,(r,c)->r|r<<c))
```
Port of [@xnor's Python 2 answer](https://codegolf.stackexchange.com/a/164577/52210).
-39 bytes thanks to *@Jakob*.
[Try it online](https://tio.run/##hVPBbtswDL33K3i0B8e1HFu2kCyXXb1iQI9FD4qjtE4dObDkDkWXb89I0VsvywqEEUU@8j1R8kG/6sVh93Lpjqdh9HDAfTr5rk@/rADg9hZ@aAwPe/DPBrZv3izaYbL@pu21c/Bdd/b9BqCz3ox73Rq4oy1AP9gnaKOPds6PRh/T@7CsG0xvwMUrBJ/R8Oe89l0Ld2DhK1zcYkOYdNv5b8QXuXQ0u6k1kWiSaEzaeLEZf43rdRvHlxV3OE3bHjvMjV6HbgdH1BchZ2efHh5BxyzOG@cja34CUTw8vmPLvDkHMVeSn@erTwDLJimapGwS2STXsYgSASgCVoVSGfwl/f@HowrYOgBFKEJfZNTkWpVCaI4olRMSoQUGpAyNVB2WkgxbV6QA5VToF@gLMkTXxEknxGpJhMUfsRVVSq4mAHFhpqIQNpJYUAm2nA88F9UFr6SsJkE5iyLaXHC8ULOsjCeLJQXzlCRgydRLycKUCqOsST9TkUySS0WUlTR1QqNJNfNkfDxSTkcvcV/WrDbHeEGzppvFuBBsFFPZhyDyqXct5zNlrIyGUIZBnP/xDYSnGy6LL@rvw71/c94c02Hy6QnftO9tZNOrX1k67CMdxzPB@fIb).
```
s->               // Method with Long-Stream parameter and long return-type
  Long.bitCount(  //  Return the amount of 1s in the binary representation of:
    s.reduce(1L,  //   Result-Long, starting at 1
     (r,c)->      //   Loop pair-wise (result,current):
      r|          //    Bitwise-OR the result `r` with:
        r<<c))    //    result `r` bitwise left-shifted by the current `c`
```
[Answer]
# Java 8, 85 bytes
Another Java port of [*xnor*'s answer](https://codegolf.stackexchange.com/a/164577). Like the original answer, this uses an arbitrary-precision bitmap so that there's no upper limit on the size of a subset sum.
It's a lambda from a sequential `java.util.stream.Stream<Integer>` to `int`.
```
s->s.reduce(java.math.BigInteger.ONE,(a,e)->a.or(a.shiftLeft(e)),(u,v)->u).bitCount()
```
[Try It Online](https://tio.run/##XVPbitswEH12vkKPMijCchzZ3kugLS0UennYx2VZtI6cderYwZJTwpJvz85IyropWGh0Zs7MGWm8VQc1367/nPfjS9tUpGqVMeSnajryNosCaKyysNVNp1qyBQYfbdPyeuwq2/Qd/xaMu8ln7KDVjj@47e57Z/VGDytGLhapyf3ZzFeGD3o9Vpo66k7ZV/652YQo/vvXV0YV0/F8pXg/UMXNa1PbH7q2VMcxoyM7gG@M@Utjv/RjZ2l8jm5n/ys/9M2a7KApCoKabvP4RNSwMTH2GIVij0@AVspoQ@5Jp/@Sf3GMi94EI@mJfZjTacnI9F1H5FfHBSOZi5KMXDyACQcL5yldoHT24ipd7jyFg4ULAVskQAkxJThSwMoUcXBkAEjpaGXhtiUuSJRjdiiVg52BLXBBdIEVUCuwJabPLkJyZErPxgCsBZ4cIUgkgZALv1LfTCAVmd9RWYGCUi8Ky6bC41kZZCX@joCS@TpLFLDwpRfSCytLd00F6velUCbKRRJ6Jd4oRsOSZaiT@PZQOba@hPOy8GpTwDO8WXwjwIXwC7EymQShjbkLGXpKvDK8BHx7/xInnMCo7gdCP4bIjdYzufEj5icvejgaq3e8Hy3fw1zatqM1V/t9e6TTr/RpGNTRhD@KujRxHN9inRl8p/M7)
Note that the combiner (the third argument to `reduce`) is unused since the stream is sequential. The function I chose is arbitrary.
[Answer]
# [Julia 0.6](http://julialang.org/), 54 52 bytes
```
V->(~W=W==[]?0:∪([n=W[] -n].+~W[2:end]))(V)|>endof
```
[Try it online!](https://tio.run/##VVFBTsMwELznFT4mwkWxY6/tSim/aA6RD5WgUlEJFYIb6rlP4A08qx8JO95AhaKVx7M7u@PN88fxsKN538/b1aY@D/3Q92N@aNfXy3c9Tv0wZrWa8v3deRjt@ml6zE1Tb5vPDcPX/fyyO9Wnt8P0fpyul6@9VlVdjUYrm7Wc/2G44U4rp5XXirQqdNJ/H66cN6XElKpURFQwp/zSKBQ6Fs6UPGPTcn1pyaxlIlmQzDomiIomxXJ4BHcJ6MtDAmPH2CC4OqI9HLOa0Nv9WghQkqhRgFmcCaC4EbEgGAkrz1hE0ckJZxGGrJjCWGuEd2mx1cqmWOJkjoeBTkZ3JMZSKguK8C@jYBN2IUKWsEtUc1Ba5rTyPDjH0z3ffRS3lnmHteJPMW@MBLjU3gwBo3ek5U2tOMMSPBaRq6aZfwA "Julia 0.6 – Try It Online")
(*-2 bytes by replacing `¬` with `~`, thanks to [Jo King](https://codegolf.stackexchange.com/questions/167872/2d-array-middle-point/167902#comment405656_167902)*)
Works for all test cases. Makes use of broadcast to collect all possible sums, then counts them.
Explanation:
```
function g_(V)
  function inner(W)  #named ~ in golf version to save bytes
    W == [] ? 0 :    #return 0 when input empty (base case)
    ∪(               #unique elements of
      [n=W[] -n]     #take the first element and its negation
      .+             #broadcast-add that to each element of
      inner([2:end]) #sign-swapping sums of the rest of the array
     )
  end                #returns the list of unique elements out of those sums
  return endof(inner(V)) #return the length of that list
end
```
---
Older solution:
# [Julia 0.6](http://julialang.org/), 64 bytes
```
N->endof(∪([2(i&2^~-j>0)-1 for i=0:~-2^(l=endof(N)),j=1:l]*N))
```
[Try it online!](https://tio.run/##VVJLasMwEN3nFFoVucggyfoGnCPkAsGBQGuwcZ0Q2m3WOULP0GPlIu48jUvoYtDzm3kzbyYZv6bhFJa@Xfb17n1@O/fycf@RByuHF3u81eNOV7UR/fkqhlZvb7U9yqnlwn1VqbE126l7Jbh8nC7ych3mz2l@3L97JTZyczBK2E7x@x/GJ26UcEp4JYIShSbCFM4UOpeqUDCl/KqMhU6FMyVP2GiqR0Em1hKRLUhiHREhFE1O5fEI6hLRl4ZEwo6wQVB1QntYJHVAb/dnIUIZWI0CzKJMBEWNAgmi4bC8xipKjl84SzBk2RTGWsO8y6stzachieM5HgYaHt0ENpZzOVCCfx4Fm7ALEbIBt0Q1RcjrHM3rwTlW9/TtE7u1xDucFT8N8cZwgMv6aQgYvVNYd9LsDEfwOES3ob/FLw "Julia 0.6 – Try It Online")
Works for input arrays with length upto 63 (so doesn't work for the last test case, which is fine according to OP).
Explanation:
```
function f_(N)
  endof(                            #length of
        ∪(                          #unique elements of
          [
           (i & 2^(j-1) > 0)        #check j'th bit (from right) of i
           * 2 - 1                  #convert bit value from (0,1)=>(-1,1)
           for i = 0:2^endof(N)-1,  #where i is numbers 0 to 2^length(N)-1
           j = 1:endof(N)           #and j is 1 to length(N) (i.e. the bits in i)
          ]                         #so we have a matrix [-1 -1 -1;1 -1 -1;1 -1 1;...]
          *                         #matrix multiply that with the input array,
          N                         #thus calculating different combinations of
         ))                         #sign-swapping sums
end
```
[Answer]
# [Desmos](https://www.desmos.com/calculator), ~~90~~ 74 bytes
*-16 bytes thanks to [Aiden Chow](https://codegolf.stackexchange.com/users/96039/aiden-chow)*!
```
f(A)=unique(∑_{j=1}^a(-1)^{floor([0...2^a]2/2^j)}A[j]).length
a=A.length
```
[Try it on Desmos!](https://www.desmos.com/calculator/macn8ggqsp)
Brute-forces all possible sums; then counts the number of distinct sums.
The exact choice of `+` or `-` is encoded in the binary digits of `i`, ignoring the zeroth bit because Desmos arrays are 1-indexed.
[Answer]
# [JavaScript (Babel Node)](https://babeljs.io/), 64 bytes
```
F=([f,...r],s=[0])=>f?F(r,s.flatMap(x=>[x+f,x])):new Set(s).size
```
[Try it online!](https://tio.run/##ZVHLboJAFN33K2YJ6RUZ5sFMG@zOXVddEhZoobEhYoS0pj9Pz2VojDZ6c/E85pzBz/qrHvbnw2lc7epd062O/XszTdsiKltKkuRc0VCUaRUXm/ZlG51pSNquHl/rU3QpNuXlsaVLFcdPx@ZbvDVjNMTJcPhpprEZxn09NKIQUU1ijEWxEfv@OPRdk3T9R7SN6pjx54c/aVRKyioS@h6bUXuDarr5gPf/XAqMIUs52OyWViRBStAeQoutyHC0uTslB@2I5RZbpuShMtxlvRZXoVeIcCiRkZBpioMAWAsQmJuX4clJ5IaE8tg5x0HOA7UD7ySkcFtYpIYeUijZaYObBZwFJmcIB1kYchkm07NkMTkdNjdzXCgLpTg2kwHXfqmVzjYNiw45hguoEK1sKOahxtdx/xDFNbkum5i1wDSrMdYvOWm4Hjfnqxv8Ni60zYBrPCtsBVzKMIz59FqIn/lsZ5c7paEZvwTDL4L/vTxz8fQL "JavaScript (Babel Node) – Try It Online")
This won't work for last testcase.
---
More effective solution (works on last testcase):
# [JavaScript (Babel Node)](https://babeljs.io/), 71 bytes
```
F=([f,...r],s=[0])=>f?F(r,[...new Set(s.flatMap(x=>[x+f,x]))]):s.length
```
[Try it online!](https://tio.run/##ZVFNT4NAEL37K/YIcUpY9oNdDfXWmyePhAOtUDUEmkK0/x7fsBiDBiYD72PmLXzUn/V4ur5fpt2xPjbdrh9em3k@FFHZUpIk14rGokyruNi3T4foSiXAvvkSL80UjUnb1dNzfYluxb683bd0q@K4ih/GpGv68/Q2T804neqxEYWIahJTLIq9OA39OHRN0g3n6BDVMeOPdz/SqJSUVSR0TFtsQe0G1bS5wPt/LgXGkKUcbLalFUmQErSH0KIrMrza/JmSg3bEcosuU/JQmW0WrzDfIUFGQqYppgCwFiAwtzTDlZPIDQnl0XPeBTkX1A68k5DCbWGRGnpIoWSnDW4W8C4wOUMYZGHIZahML5LV5HTonMxxoCyE4rWZDLj2a6x0sWlYdNhjOIAKq5UNwTzUuB3nD6s4JsdlE7MWmGY1yvp1TxqOx8n56AbvxoW0GXCNZ4WugEsZijGf/gbiZ57t7HqmNCTjj2D4Q/CvyzMXz98 "JavaScript (Babel Node) – Try It Online")
---
This won't work in a real browser due to [`Array#smoosh`](https://github.com/tc39/proposal-flatMap/pull/56).
Thanks to Bubbler, `[x+f,x-f]` -> `[x+f,x]` saves 2 bytes.
]


[Question]
      [
This is the Collatz Conjecture (OEIS [A006577](https://oeis.org/A006577)):
* Start with an integer *n* > 1.
* Repeat the following steps:
	+ If *n* is even, divide it by 2.
	+ If *n* is odd, multiply it by 3 and add 1.
It is proven that for all positive integers up to *5 \* 260*, or about *5764000000000000000*, *n* will eventually become *1*.
Your task is to find out how many iterations it takes (of halving or tripling-plus-one) to reach *1*.
[Relevant xkcd](http://xkcd.com/710/) :)
Rules:
* Shortest code wins.
* If a number < 2 is input, or a non-integer, or a non-number, output does not matter.
Test cases
```
2  -> 1
16 -> 4
5  -> 5
7  -> 16
```

[Answer]
# C - ~~50~~ 47 characters
Poor little C unfortunately requires an awful amount of code for basic I/O, so shorting all that down has made the UI slightly unintuitive.
```
b;main(a){return~-a?b++,main(a&1?3*a+1:a/2):b;}
```
Compile it with for example `gcc -o 1 collatz.c`. The input is in unary with space-separated digits, and you will find the answer in the exit code. An example with the number 17:
```
$> ./1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
$> echo $?
12
$>
```
[Answer]
## GolfScript, ~~24~~ ~~23~~ ~~21~~ ~~20~~ 18 chars
```
~{(}{3*).2%6\?/}/,
```
Assumes input on stdin. [Online test](http://golfscript.apphb.com/?c=O1syIDE2IDUgN117YAp%2Beyh9ezMqKS4yJTZcPy99LywKcH0v)
[Answer]
# [FRACTRAN](https://en.wikipedia.org/wiki/FRACTRAN), 8 fractions (45 bytes)
$$\frac{5120}{33}, \frac{15}{11}, \frac{22}{105}, \frac{33}{560}, \frac{13}{5}, \frac{3}{2}, \frac{7}{9}, \frac{11}{7}$$
Takes input as \$3^n\$; halts at \$3 \cdot 13^m\$ for a sequence with \$m\$ iterations.
[Try it online!](https://tio.run/##DcVBCoAwDATAr@QDkm6WWPyMUMSCl1Zq3290LlNHOeYoLcJhSUmBKyBmiuRCqq9J8CdUk6ybAJojgnt@@z2v3p5YTvAD "FRACTRAN – Try It Online")
### How it works
If the current state is \$3^n \cdot 13^m\$, if \$n = 1\$, the program halts immediately; if \$n = 2k\$ is even, we have
$$\begin{multline\*}3^{2k} \cdot 13^m
\xrightarrow{\left(\frac{7}{9}\right)^k}
7^k \cdot 13^m
\xrightarrow{\frac{11}{7}}
7^{k - 1}\cdot 11 \cdot 13^m
\xrightarrow{\frac{15}{11}}
3 \cdot 5 \cdot 7^{k - 1} \cdot 13^m
\\
\xrightarrow{\left(\frac{22}{105} \cdot \frac{15}{11}\right)^{k - 1}}
2^{k - 1} \cdot 3 \cdot 5 \cdot 13^m
\xrightarrow{\frac{13}{5}}
2^{k - 1} \cdot 3 \cdot 13^{m + 1}
\xrightarrow{\left(\frac{3}{2}\right)^{k - 1}}
3^k \cdot 13^{m + 1};\end{multline\*}$$
and if \$n = 2k + 1\$ is odd, we have
$$\begin{multline\*}
3^{2k + 1} \cdot 13^m
\xrightarrow{\left(\frac{7}{9}\right)^k}
3 \cdot 7^k \cdot 13^m
\xrightarrow{\frac{11}{7}}
3 \cdot 7^{k - 1} \cdot 11 \cdot 13^m
\xrightarrow{\frac{5120}{33}}
2^{10} \cdot 5 \cdot 7^{k - 1} \cdot 13^m
\\
\xrightarrow{\left(\frac{33}{560} \cdot \frac{5120}{33}\right)^{k - 1}}
2^{6k + 4} \cdot 5 \cdot 13^m
\xrightarrow{\frac{13}{5}}
2^{6k + 4} \cdot 13^{m + 1}
\xrightarrow{\left(\frac{3}{2}\right)^{6k + 4}}
3^{6k + 4} \cdot 13^{m + 1},\end{multline\*}$$
where \$6k + 4 = 3n + 1\$.
[Answer]
## Perl 34 (+1) chars
```
$\++,$_*=$_&1?3+1/$_:.5while$_>1}{
```
Abusing `$\` for final output, as per usual. Run with the `-p` command line option, input is taken from `stdin`.
Saved one byte due to [Elias Van Ootegem](https://codegolf.stackexchange.com/posts/comments/162982?noredirect=1). Specifically, the observeration that the following two are equivalent:
```
$_=$_*3+1
$_*=3+1/$_
```
Although one byte longer, it saves two bytes by shortening `$_/2` to just `.5`.
Sample usage:
```
$ echo 176 | perl -p collatz.pl
18
```
---
## PHP 54 bytes
```
<?for(;1<$n=&$argv[1];$c++)$n=$n&1?$n*3+1:$n/2;echo$c;
```
Javascript's archnemesis for the Wooden Spoon Award seems to have fallen a bit short in this challenge. There's not a whole lot of room for creativity with this problem, though. Input is taken as a command line argument.
Sample usage:
```
$ php collatz.php 176
18
```
[Answer]
# Mathematica (35)
```
If[#>1,#0@If[OddQ@#,3#+1,#/2]+1,0]&
```
Usage:
```
If[#>1,#0[If[OddQ@#,3#+1,#/2]]+1,0]&@16
>> 4
```
[Answer]
**Java, 165, 156, 154,134,131,129,128,126** (verbose languages need some love too)
```
class a{public static void main(String[]a){for(int x=Short.valueOf(a[0]),y=0;x>1;x=x%2<1?x/2:x*3+1,System.out.println(++y));}}
```
All is done inside the for
```
for(int x=Short.valueOf(a[0]),y=0;x>1;x=x%2<1?x/2:x*3+1,System.out.println(++y))
```
That's freaking beautiful man. Thanks to Pater Taylor!!!, and the idea of using a for loop was stolen from ugoren
I replaced Integer for Short.
[Answer]
As I usually do, I will start the answers off with my own.
## JavaScript, ~~46~~ 44 chars (run on console)
```
for(n=prompt(),c=1;n>1;n=n%2?n*3+1:n/2,++c)c
```
[Answer]
## Python repl, 48
I'm not convinced that there isn't a shorter expression than `n=3*n+1;n/=1+n%2*5;`. I probably found a dozen different expressions of all the same length...
```
i=0
n=input()
while~-n:n=3*n+1;n/=1+n%2*5;i+=1
i
```
edit: I've found another solution that will never contend, but is too fun not to share.
```
s='s'
i=s
n=i*input()
while 1:
 while n==n[::2]+n[::2]:i+=s;n=n[::2]
 if n==s:i.rindex(s);break
 n=3*n+s
 i+=s
```
[Answer]
### [Rebmu](http://hostilefork.com/rebmu): 28
```
u[++jE1 AeEV?a[d2A][a1M3a]]j
```
On a problem this brief and mathy, GolfScript will likely win by some percent against Rebmu (if it's not required to say, read files from the internet or generate JPG files). Yet I think most would agree the logic of the Golfscript is nowhere near as easy to follow, and the total executable stack running it is bigger.
Although Rebol's own creator [Carl Sassenrath](http://en.wikipedia.org/wiki/Carl_Sassenrath) told me he found Rebmu "unreadable", he is busy, and hasn't time to really practice the pig-latin-like transformation via [unmushing](https://github.com/hostilefork/rebmu/blob/2965e013aa6e0573caa326780d97522a425103f0/mushing.rebol#L18). This really is merely transformed to:
```
u [
    ++ j
    e1 a: e ev? a [
        d2 a
    ] [
        a1 m3 a
    ]
]
j
```
Note that the space was required to get an **a:** instead of an **a**. This is a "set-word!" and the evaluator notices that symbol type to trigger assignment.
If it were written in unabbreviated (yet awkwardly-written Rebol), you'd get:
```
until [
    ++ j
    1 == a: either even? a [
        divide a 2
    ] [
        add 1 multiply 3 a
    ]
 ]
 j
```
Rebol, like Ruby, evaluates blocks to their last value. The UNTIL loop is a curious form of loop that takes no loop condition, it just stops looping when its block evaluates to something not FALSE or NONE. So at the point that `1 ==` the result of assigning A (the argument to rebmu) to the result of the Collatz conditional (either is an IF-ELSE which evaluates to the branch it chooses)... the loop breaks.
J and K are initialized to integer value zero in Rebmu. And as aforementioned, the whole thing evaluates to the last value. So a J reference at the end of the program means you get the number of iterations.
**Usage:**
```
>> rebmu/args [u[++jE1 AeEV?a[d2A][a1M3a]]j] 16
== 4
```
[Answer]
### J, 30 characters
```
<:#-:`(1+3&*)`]@.(2&|+1&=)^:a:
```
Turned out quite a bit longer than desired
usage:
```
   <:#-:`(1+3&*)`]@.(2&|+1&=)^:a:2
1
   <:#-:`(1+3&*)`]@.(2&|+1&=)^:a:16
4
   <:#-:`(1+3&*)`]@.(2&|+1&=)^:a:5
5
   <:#-:`(1+3&*)`]@.(2&|+1&=)^:a:7
16
   <:#-:`(1+3&*)`]@.(2&|+1&=)^:a:27
111
```
* `-:`(1+3&*)`]` is a gerund composed of three verbs, used on three occasions. `-:` means "halve", `(1+3&*)` or `(1+3*])` encodes the multiplication step and `]` (identity) aids termination.
* `2&|+1&=` forms an index to the gerund. literally, "the remainder after division by two plus whether it equals one".
* `#verb^:a:` iterates the function until the result is stable (here, forced explicitly), while collecting the steps, then counts them. [Stolen from @JB](https://codegolf.stackexchange.com/a/1257/7209). `<:` decrements the step count by one to align with the question requirements.
[Answer]
## [Brachylog](https://github.com/JCumin/Brachylog), 16 bytes
```
1b|{/₂ℕ|×₃+₁}↰+₁
```
[Try it online!](https://tio.run/##SypKTM6ozMlPN/r/qG3Do6bGh1snlP83TKqp1n/U1PSoZWrN4emPmpq1gTK1QAUg@v//aFMdMx1zHQsdI4tYAA "Brachylog – Try It Online")
### Explanation
```
         Either:
  1        The input is 1.
  b        In which case we unify the output with 0 by beheading the 1
           (which removes the leading digit of the 1, and an "empty integer"
           is the same as zero).
|        Or:
  {        This inline predicate evaluates a single Collatz step on the input.
           Either:
    /₂       Divide the input by 2.
    ℕ        And ensure that the result is a natural number (which is
             equivalent to asserting that the input was even).
  |        Or:
    ×₃+₁     Multiply the input by 3 and add 1.
  }
  ↰        Recursively call the predicate on this result.
  +₁       And add one to the output of the recursive call.
```
An alternative solution at the same byte count:
```
;.{/₂ℕ|×₃+₁}ⁱ⁾1∧
```
[Try it online!](https://tio.run/##SypKTM6ozMlPN/r/qG3Do6bGh1snlP@31qvWf9TU9Khlas3h6Y@amrWBErWPGjc@atxn@Khj@f//0aY6ZjrmOhY6RhY6FqaxAA "Brachylog – Try It Online")
```
;.          The output of this is a pair [X,I] where X is the input and
            I will be unified with the output.
{/₂ℕ|×₃+₁}  This is the Collatz step predicate we've also used above.
ⁱ⁾          We iterate this predicate I times on X. Since we haven't actually
            specified I, it is still a free variable that Brachylog can backtrack
            over and it will keep adding on iterations until the next
            constraint can be satisfied.
1           Require the result of the iteration to be 1. Once this is
            satisfied, the output variable will have been unified with
            the minimum number of iterations to get here.
∧           This AND is just used to prevent the 1 from being implicitly
            unified with the output variable as well.
```
[Answer]
# [FRACTRAN](https://en.wikipedia.org/wiki/FRACTRAN), 24 fractions
Uses 180 bytes, for the more conventional counters...
```
68/13, 133/102, 341/51, 115/17, 17/19, 87/161, 17/23, 23/29, 53/93,
26973/217, 410/259, 43/111, 976/37, 37/41, 329/215, 37/43, 43/47,
118/265, 1/53, 53/59, 67/305, 1/61, 61/67, 117/4
```
[Try it online!](https://tio.run/##JY1RDsMgDEOv0gMwBScQ4DKTqmmT9tNOXc8/ZuiX5RfHfh3r4zzWrXevAgsLzARRw2IJkkGALCjUImhhqRTHtMq4mihpNmnDeSsEI54QRTNPiX3gQysuxoMVSbSmjcF8eZuxNFZQRZ2Y4zZ7R4cXsTjhmHbKjPKz9673pL/9c7737dtvT@AP "FRACTRAN – Try It Online")
### Explanation
Am a bit lazy to add an explanation now; happy to do so if it gets attention/upvotes! However, I do have some notes for when I first wrote up the Collatz Conjecture code which you can run [here](https://tio.run/##JY1RDsMgDEOvwgGoQhIIcJlJ1bRJ@2mrrudfZuiX9RzHfp/r8zrXzd0ascbAqsRJYlCmwmAuxBVaiXsMDWI8UZAWJYFblPog6xXGiGdOJAWnjDrGQ69GioNWykCVjmC5WWcsjxVuJAYb4zp7R4dV0jTNMW2QGcWnu8sjy28/rs@@fX05xJeX/AE). It's almost the same, but the TIO command line arguments are set to print every number in the sequence along the way, which makes it less of a blackbox!
### State Diagrams for COLLATZGAME
Here are the state diagrams, for which I used Conway's original notation which he presents in this [article](https://link.springer.com/chapter/10.1007/978-1-4612-4808-8_2).
[![Original CollatzGame](https://i.stack.imgur.com/ad8sYm.jpg)](https://i.stack.imgur.com/ad8sYm.jpg)[![Cleaned-up CollatzGame](https://i.stack.imgur.com/MhQKCm.jpg)](https://i.stack.imgur.com/MhQKCm.jpg)
### Change Made
The above is simply to calculate the Collatz sequence for `n` given an input of the form `2^n`. The only change I made to also keep count of the steps taken was to make the 1/3 from states Q -> D into an 11/3, 11 being the smallest unused prime. This fraction is only executed once for every number in the sequence; it's the state that figures out whether the number is even or odd to figure out what's next. Therefore, the `11` prime register is incremented once per number in the sequence, except one, yielding the number of steps.
### Note
I simply encoded the state diagram as below and wrote an interpreter which did the dirty work. However, the work done to convert a state diagram to FRACTRAN is also detailed in Conway's article above:
* A: 9/4 -> T
* T: 4/1 -> Q
* Q: 7/6\*, 11/3 -> D, 5/1 -> R
* R: 3/7\*|Q
* D: 1/3 -> E, 729/7 -> M
* M: 10/7\*, 1/3 -> N, 16/1 -> O
* N: 7/5\*|M
* E: 2/5\*|A
* O: 1/5\*|A
[Answer]
## APL (31)
```
A←0⋄A⊣{2⊤⍵:1+3×⍵⋄⍵÷2}⍣{⍺=A+←1}⎕
```
[Answer]
## 80386 assembly, 16 bytes
This example uses AT&T syntax and the fastcall calling convention, the argument goes into `ecx`:
```
collatz:
        or $-1,%eax              # 3 bytes, eax = -1;
.Loop:  inc %eax                 # 1 byte,  eax += 1;
        lea 1(%ecx,%ecx,2),%edx  # 4 bytes, edx = 3*ecx + 1;
        shr %ecx                 # 2 bytes, CF = ecx & 1;
                                 #          ecx /= 2;
                                 #          ZF = ecx == 0;
        cmovc %edx,%ecx          # 3 bytes, if (CF) ecx = edx;
        jnz .Loop                # 2 bytes, if (!ZF) goto .Loop;
        ret                      # 1 byte,  return (eax);
```
Here are the resulting 16 bytes of machine code:
```
83 c8 ff 40 8d 54 49 01 d1 e9 0f 42 ca 75 f4 c3
```
[Answer]
## PowerShell: 77 74 71 70 61
**Golfed code:**
```
for($i=(read-host);$i-ne1;$x++){$i=(($i/2),(3*$i+1))[$i%2]}$x
```
---
**Notes:**
I originally tried taking the user input without forcing it to an integer, but that broke in an interesting way. Any odd inputs would process inaccurately, but even inputs would work fine. It took me a minute to realize what was going on.
When performing multiplication or addition, PowerShell treats un-typed input as a string first. So, `'5'*3+1` becomes '5551' instead of 16. The even inputs behaved fine because PowerShell doesn't have a default action for division against strings. Even the even inputs which would progress through odd numbers worked fine because, by the time PowerShell got to an odd number in the loop, the variable was already forced to an integer by the math operations anyway.
>
> Thanks to [Danko Durbic](https://codegolf.stackexchange.com/questions/12177/collatz-conjecture/15220?noredirect=1#comment29077_15220) for pointing out I could just invert the multiplication operation, and not have to cast `read-host` to int since PowerShell bases its operations on the first object.
>
>
>
PowerShell Golfer's Tip: For some scenarios, like this one, `switch` beats `if/else`. Here, the difference was 2 characters.
>
> Protip courtesy of [Danko Durbic](https://codegolf.stackexchange.com/questions/12177/collatz-conjecture/15220?noredirect=1#comment29076_15220): For this particular scenario, an array can be used instead of `switch`, to save 8 more characters!
>
>
>
There's no error checking for non-integer values, or integers less than two.
If you'd like to audit the script, put `;$i` just before the last close brace in the script.
I'm not sure exactly how well PowerShell handles numbers that progress into very large values, but I expect accuracy is lost at some point. Unfortunately, I also expect there's not much that can be done about that without seriously bloating the script.
---
**Ungolfed code, with comments:**
```
# Start for loop to run Collatz algorithm.
# Store user input in $i.
# Run until $i reaches 1.
# Increment a counter, $x, with each run.
for($i=(read-host);$i-ne1;$x++)
{
    # New $i is defined based on an array element derived from old $i.
    $i=(
        # Array element 0 is the even numbers operation.
        ($i/2),
        # Array element 1 is the odd numbers operation.
        (3*$i+1)
    # Array element that defines the new $i is selected by $i%2.
    )[$i%2]
}
# Output $x when the loop is done.
$x
# Variable cleanup. Don't include in golfed code.
rv x,i
```
---
**Test cases:**
Below are some samples with auditing enabled. I've also edited the output some for clarity, by adding labels to the input and final count and putting in spacing to set apart the Collatz values.
```
---
Input: 2
1
Steps: 1
---
Input: 16
8
4
2
1
Steps: 4
---
Input: 5
16
8
4
2
1
Steps: 5
---
Input: 7
22
11
34
17
52
26
13
40
20
10
5
16
8
4
2
1
Steps: 16
---
Input: 42
21
64
32
16
8
4
2
1
Steps: 8
---
Input: 14
7
22
11
34
17
52
26
13
40
20
10
5
16
8
4
2
1
Steps: 17
---
Input: 197
592
296
148
74
37
112
56
28
14
7
22
11
34
17
52
26
13
40
20
10
5
16
8
4
2
1
Steps: 26
---
Input: 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
Steps: 106
---
Input: 6174
3087
9262
4631
13894
6947
20842
10421
31264
15632
7816
3908
1954
977
2932
1466
733
2200
1100
550
275
826
413
1240
620
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
Steps: 111
---
Input: 8008135
24024406
12012203
36036610
18018305
54054916
27027458
13513729
40541188
20270594
10135297
30405892
15202946
7601473
22804420
11402210
5701105
17103316
8551658
4275829
12827488
6413744
3206872
1603436
801718
400859
1202578
601289
1803868
901934
450967
1352902
676451
2029354
1014677
3044032
1522016
761008
380504
190252
95126
47563
142690
71345
214036
107018
53509
160528
80264
40132
20066
10033
30100
15050
7525
22576
11288
5644
2822
1411
4234
2117
6352
3176
1588
794
397
1192
596
298
149
448
224
112
56
28
14
7
22
11
34
17
52
26
13
40
20
10
5
16
8
4
2
1
Steps: 93
---
```
Interesting bits about the input numbers which are not from the question's test cases:
* `14` and `197` are [Keith Numbers](http://numberphile.com/videos/197_keith.html)
* `31` is a [Mersenne Prime](http://numberphile.com/videos/31.html)
* `6174` is [Kaprekar's Constant](http://numberphile.com/videos/6174.html)
* And lastly, I just like `8008135`. And [Numberphile](http://numberphile.com), obviously.
[Answer]
# [brainfuck](https://github.com/TryItOnline/brainfuck), ~~59~~ 56 bytes
```
,-[<->[[>]+<[-<]>>]>[-<<[++>+<]>->]<<[+>+++<]<<+>>>]<<<.
```
[Try it online!](https://tio.run/##dVFNT8MwDL33V/gIyoqYhBBSrfwG7lEOWeNt0bpkSrIy@PPF7lrtAr7En8/vObvsQtxf@9O0MVrNZlpsF9MWcU2LYWu13licWoOtNkZbhdzOSav5RaOUVhzeB5VMIntKa0ng9JlDrAUc9DQMMLrhSvBU3Yki7HM6A5U0uHgosNvDxR3ouTE8Pc@3AtAY9hQ7wkNJ2KKB7usYBlrg6BZKLQ0A4IO3hu7MW3jx9lVKItEwMJMzrFc4z5EWs9D5MAa/Iu6@lymWKrUUK0QiD/UYyqxEiuuq9Vwovc57ePuAmiDTmQ/tKd@BZhXSUWrKxEj0b8eBZN2t3tlIUevGom344tDRUGhhk2DR@EM5zSD4B6nGNt1FvgFc9NAP5DKQ648wM/EPGhAiByPlQrzqhbXzJ0/b918) (Slightly modified for ease of use)
Input and output as character codes. This is more useful with arbitrarily sized cells, but can still work with small values in limited cell sizes.
### How It Works
```
Tape Format:
Counter 0 Copy Number Binary...
^End           ^Start
,-[ Get input, decrement by 1 and start loop
  <->                  Initialises the copy of the value at -1
  [[>]+<[-<]>>]        Converts the input to binary while preserving a negative copy
  <+>>[-<<[++>+<]>->] If the last digit of the binary is 1 (n-1 is odd), divide by 2 and decrement
  <<[+>+++<]            If the last digit of the binary is 0 (n-1 is even), multiply by 3
  <<+>>>               Increment counter and end on n-1
]<<<.                 End loop and print counter
```
[Answer]
# [Jelly](https://github.com/DennisMitchell/jelly), 10 bytes
```
×3‘ƊHḂ?Ƭi2
```
[Try it online!](https://tio.run/##ASgA1/9qZWxsef//w5cz4oCYxopI4biCP8asaTL/w4figqz//zIsMTYsNSw3 "Jelly – Try It Online")
### How it works
```
×3‘ƊHḂ?Ƭi2    Main link (monad). Input: integer >= 2
      ?       Create a "ternary-if" function:
     Ḃ          If the input is odd,
×3‘Ɗ            compute 3*n+1;
    H           otherwise, halve it.
       Ƭ      Repeat until results are not unique; collect all results
        i2    Find one-based index of 2
```
Example: The result of `...Ƭ` for input 5 is `[5, 16, 8, 4, 2, 1]`. The one-based index of 1 is 6, which is 1 higher than expected. So we choose the index of 2 (which is guaranteed to come right before 1) instead.
[Answer]
# ngn/k, 21 bytes
```
#{(2|-2!x;1+3*x)2!x}\
```
I converted the operation to be `x = max(2, x/2)` so the sequence had a fixed point, and converges (`\`) handled the rest. `#` counts the number of steps, including the beginning.
[Answer]
# [Jelly](http://github.com/DennisMitchell/jelly), 12 [bytes](https://github.com/DennisMitchell/jelly/wiki/Code-page)
```
×3‘$HḂ?ß0’?‘
```
[Try it online!](http://jelly.tryitonline.net/#code=w5cz4oCYJEjhuII_w58w4oCZP-KAmA&input=&args=Nw)
### How it works
```
×3‘$HḂ?ß0’?‘  Main link. Argument: n (integer)
     Ḃ?       Yield the last bit of n is 1:
   $            Evaluate the three links to the left as a monadic chain:
×3                Multiply n by 3.
  ‘               Increment the product by 1.
    H           Else, halve n.
         ’?   If n-1 is non-zero:
       ß        Recursively call the main link.
        0     Else, yield 0.
           ‘  Increment the result by 1.
```
[Answer]
# Gambit scheme, ~~106~~ 98 characters, 40 parentheses
```
(let((f(lambda(x)(cond((= x 1) 0)((odd? x)(+ 1(f(+ 1(* 3 x)))))(else(+ 1(f(/ x 2))))))))(f(read)))
```
**~~91~~ 89 chars** with define directly
```
(define(f x)(cond((= x 1)0)((odd? x)(+ 1(f(+ 1(* 3 x)))))(else(+ 1(f(/ x 2))))))(f(read))
```
[Answer]
# Python 2, ~~68~~ ~~58~~ ~~54~~ 52 bytes
```
f=lambda n:1+(n-2and f((n/2,3*n+1)[n%2]));f(input())
```
Thanks to @Bakuriu and @boothby for the tips :)
[Answer]
## GolfScript (23 chars)
```
~{.1&{.3*)}*.2/.(}do;],
```
[Online test](http://golfscript.apphb.com/?c=O1syIDE2IDUgN117YAp%2Bey4xJnsuMyopfSouMi8uKH1kbztdLApwfS8%3D)
[Answer]
## F# - 65 chars
```
let rec c n=function 1->n|i->c(n+1)(if i%2=0 then i/2 else i*3+1)
```
[Answer]
# dc, 27 characters
Applying [boothby's black magic](https://codegolf.stackexchange.com/a/12209/4372):
```
?[d3*1+d2%5*1+/d1<x]dsxxkzp
```
I'm not really sure if I understand how - or *that* - it works.
Usage:
```
$ dc collatz.dc <<< 7
16
```
# dc, 36 characters
My own creation; a somewhat more traditional approach, even tho I had to wrangle with the language a fair bit to overcome the lack of an `else` part to `if` statements:
```
?[2/2Q]se[dd2%[0=e3*1+]xd1<x]dsxxkzp
```
Internally it produces all numbers of the sequence and stores them on the stack, then pops the final `1` and displays the stack height.
[Answer]
# Ruby 1.9, 49 characters
Rubyfied Valentin CLEMENT's [Python answer](https://codegolf.stackexchange.com/a/12195/4372), using the stabby lambda syntax. Sqeezed it into one statement for added unreadability.
```
(f=->n{n>1&&1+f[[n/2,3*n+1][n%2]]||0})[gets.to_i]
```
Some overhead because Ruby, unlike Python, is not happy about mixing numbers with booleans.
[Answer]
# [Retina](https://github.com/mbuettner/retina), 43 bytes
```
11
2
(2+)1
$1$1$0$0$0$0
2.*
$0x
)`2
1
1?x
1
```
Takes input and prints output in unary.
Each line should go to its own file. 1 byte per extra file added to byte-count.
You can run the code as one file with the `-s` flag. E.g.:
```
> echo -n 1111111|retina -s collatz
1111111111111111
```
The algorithm is a loop of doing a Collatz step with the unary number and adding a new step-marker `x` at the end of the string if the number isn't 1.
When the loop ends with `1`, we convert the markers to a unary number (removing the leading `1`) which is the desired output.
[Answer]
# [Hexagony](https://github.com/m-ender/hexagony), ~~48~~ 44 bytes
```
?(]$_)"){{?{*')}/&!/={:<$["/>&_(.<@2'%<>./>=
```
[Try it online!](https://tio.run/##y0itSEzPz6v8/99eI1YlXlNJs7ravlpLXbNWX01R37baykYlWknfTi1eQ8/GwUhd1cZOT9/O9v9/I3MA "Hexagony – Try It Online")
Expanded:
```
     ? ( ] $ _
    ) " ) { { ?
   { * ' ) } / &
  ! / = . { < $ [
 " / > & _ ( . < @
  2 ' % < > : / >
   = . . . . . .
    . . . . . .
     . . . . .
```
 ~~Note that this fails on `1` for uhh... *reasons*. Honestly, I'm not really sure how this works anymore. All I know is that the code for odd numbers is run backwards for even numbers? Somehow?~~
The new version is much cleaner than the previous, but has a few more directionals in comparison and also ends in a divide-by-zero error. The only case it doesn't error is when it actually handles `1` correctly.
[Answer]
# ><>, 27 26 23 bytes
```
\ln;
\::2%:@5*1+2,*+:2=?
```
Like the other ><> answers, this builds the sequence on the stack. Once the sequence reaches 2, the size of the stack is the number of steps taken.
Thanks to @Hohmannfan, saved 3 bytes by a very clever method of computing the next value directly. The formula used to calculate the next value in the sequence is:
$$f(n)=n\cdot\frac{5(n\bmod2)+1}{2}+(n\bmod2)$$
The fraction maps even numbers to 0.5, and odd numbers to 3. Multiplying by `n` and adding `n%2` completes the calculation - no need to choose the next value at all!
**Edit 2:** Here's the pre-@Hohmannfan version:
```
\ln;
\:::3*1+@2,@2%?$~:2=?
```
The trick here is that both `3n+1` and `n/2` are computed at each step in the sequence, and the one to be dropped from the sequence is chosen afterwards. This means that the code doesn't need to branch until 1 is reached, and the calculation of the sequence can live on one line of code.
**Edit:** Golfed off another character after realising that the only positive integer that can lead to 1 is 2. As the output of the program doesn't matter for input < 2, the sequence generation can end when 2 is reached, leaving the stack size being the exact number of steps required.
Previouser version:
```
\~ln;
\:::3*1+@2,@2%?$~:1=?
```
[Answer]
# Julia 0.6, ~~29~~ 27 bytes
```
!n=n>1&&1+!(n%2>0?3n+1:n/2)
```
I can't seem to compile Julia 0.1 on my machine, so there's a chance this is non-competing.
[Try it online!](http://julia.tryitonline.net/#code=IW49bj4xJiYxKyEobiUyPjA_M24rMTpuLzIpCgpwcmludGxuKCEyKQpwcmludGxuKCExNikKcHJpbnRsbighNSkKcHJpbnRsbighNyk&input=)
[Answer]
## C, 70 69 chars
Quite simple, no tricks.
Reads input from stdin.
```
a;
main(b){
    for(scanf("%d",&b);b-1;b=b%2?b*3+1:b/2)a++;
    printf("%d",a);
}
```
]