[Question]
  [
# Challenge
Given a binary number as input through any means, "simplify" the number using a full program or a function.
# Input
```
[binary]
```
* `binary` is a number in binary that is over 0.
# Output
Take the input, convert it to base 10 without using a builtin, then if that number contains only 1s and 0s, convert it into a base 10 number as if it were another binary number. Repeat the process until the number cannot be read in binary and output that number.
# Other information
* If the input is 1, simply output `1`. Your program should not go on infinitely simplifying 1.
* This is code golf, so shortest answer in bytes by Tuesday (November 17th) wins.
* If anything is confusing, leave a comment specifying what I need to clear up and I will edit it accordingly.
* Builtins for base conversion are not allowed.
# Examples
```
     Input | Output
         1 | 1
      1010 | 2
      1011 | 3
   1100100 | 4
   1100101 | 5
1111110011 | 3
```
  
[Answer]
# Pyth, ~~20~~ 16 bytes
```
u?-GTG`u+yNsTG0z
```
*4 bytes thanks to Jakube*
Half of the code (`u+yNsTG0`) is simply the base conversion code.
[Test Suite](https://pyth.herokuapp.com/?code=u%3F-GTG%60u%2ByNsTG0z&test_suite=1&test_suite_input=1%0A1010%0A1011%0A1100100%0A1100101%0A1010101&debug=0)
```
u?-GTG`u+yNsTG0z
                    z = input() (The string of 1s and 0s)
                    T = 10
u              z    Apply until the value stops changing, starting with z
                    G is the current value, a string of 0s and 1s.
 ?-GT               If G - T, e.g., G with the digits 1 and 0 removed is not empty,
     G              Return G, to end the iteration.
       u     G0     Else, reduce over G with initial value 0.
         yN         Double the running total
        +  sT       and add the next digit, cast to an int.
      `             Convert to string.
```
The input `1` is handled by the fact that `u` notices the value has stopped changing.
[Answer]
# CJam, ~~24~~ 23 bytes
```
q{:~{1$++}*s__,(As*-!}g
```
Try it online in the [CJam interpreter](http://cjam.aditsu.net/#code=q%7B%3A~%7B1%24%2B%2B%7D*s__%2C(As*-!%7Dg&input=1100101).
### How it works
```
q                        Read all input.
 {                   }g  Do:
  :~                       Evaluate each character. Maps '0' -> 0 and '1' -> 1.
    {    }*                Fold; for each integer but the first:
     1$                      Copy the second-topmost integer.
       ++                    Add all three integers on the stack.
           s__             Cast to string and push two copies.
              ,(           Calculate string length and subtract 1.
                As         Push the string "10".
                  *        Repeat the string length-1 times.
                   -       Remove its elements from the string representation
                           of the integer.
                    !      Apply logical NOT.
                         If `!' pushed 1, repeat the loop.
```
[Answer]
## Pip, ~~28~~ 27 bytes
```
Ta=1|aRMta:$+(^a)*2**RV,#aa
```
Takes input as a command-line argument. We want to loop until `a=1` or `a` contains some character(s) besides 0's and 1's. This latter condition is tested by `RM`'ing all characters in `t` = `10` from `a`. If there's anything left, the condition is truthy.
Inside the loop, the conversion works as follows:
```
a:$+(^a)*2**RV,#a
              ,#a  range(len(a))
            RV     reversed
         2**       2 to the power of each element
    (^a)*          multiplied item-wise with each digit in split(a)
  $+               Sum
a:                 and assign back to a
```
Putting `a` at the end auto-prints it.
A recursive solution in 28 bytes:
```
a<2|aRMt?a(f$+(^a)*2**RV,#a)
```
[Answer]
## Python 2, 52
```
f=lambda n:n>1<'2'>max(`n`)and f(n%10+2*f(n/10))or n
```
It's easier to think of this as two recursive functions:
```
g=lambda n:n and n%10+2*g(n/10)
f=lambda n:n>1<'2'>max(`n`)and f(g(n))or n
```
The function `g` converts a decimal value to binary, and the function `f` applies `g` repeatedly is long as its argument is made of digits 0 and 1 (`'2'>max(`n`)`) and is not `1`. The golfed code collapses them into a single function by inserting the definition of `g(n)` for `f(n)`, replacing the recursive call to `g` with `f`. The base case of `n=0` of `g` is automatically handled by the check `n>1`.
[Answer]
# Prolog, ~~220~~ 212 bytes
```
:-use_module(library(clpfd)).
x(B,N):-reverse(B,C),foldl(y,C,0-0,_-N).
y(B,J-M,I-N):-B in 0..1,N#=M+B*2^J,I#=J+1.
b(N,I):-N>47,N<50,I is(N-48).
p(N):-N>1,number_codes(N,L),maplist(b,L,Y),x(Y,B),p(B);write(N).
```
**Explanation**  
**p** is the main function and performs the following steps (with help from b,x,y):
* checks if current number is bigger than 1
* converts integer to list of ascii representations of digits
* checks that all numbers are 0 or 1
* converts ascii list to binary integer list
* converts binary integer list to decimal number
* recurses
* prints when a predicate fails.
**Edit:** Saved 8 bytes by unifying the p-clauses with OR.
[Answer]
# Mathematica ~~107~~ 106
With a byte saved by DLosc.
```
j@d_:=(p=0;v=IntegerDigits@d;
Which[d<2,1,Complement[v,{0,1}]=={},j@Fold[#+#2 2^p++&,0,Reverse@v],1<2,d])
```
Break the input into its digits.
If the input is 1, output 1.
If the input is a number consisting of 0's and 1's, convert that to decimal and run it through again.
Otherwise, return the input.
---
```
j[1]
```
> 
> 1
> 
> 
> 
---
```
j[11010001]
```
> 
> 209
> 
> 
> 
---
```
j[1111110001]
```
> 
> 1009
> 
> 
> 
---
```
j[1111110011]
```
> 
> 3
> 
> 
> 
The first step yields 1011 which in turn yields 3.
---
Here we test starting with 1011.
```
j[1011]
```
> 
> 3
> 
> 
> 
[Answer]
# Javascript, 132, 123 bytes
Well, it's not the best answer, but..
FYI, if an invalid input is given, it displays the same to the user.
```
function c(x){while(x!=0&&!/[2-9]/.test(x)){for(i=r=0;x;i++)r+=x%10*Math.pow(2,i),x=parseInt(x/10);x=r}alert(x)}c(prompt())
```
[Answer]
# JavaScript ES6, 52
As a function. The function argument must be either a string of binary digits or a number whose decimal representation contains only 1 and 0. 
Test running the snippet below in an EcmaScript 6 compliant browser - implementing arrow functions, template strings and spread operator (I use Firefox)
```
f=s=>s<2|[...s+''].some(c=>(n+=+c+n,c>1),n=0)?s:f(n)
// To test
console.log=(...x)=>O.innerHTML+=x+'\n';
// Basic test cases
;[[1,1],[1010,2],[1011,3],[1100100,4],[1100101,5],[1111110011,3]]
.forEach(t=>console.log(t[0]+' -> '+f(t[0])+' expected '+t[1]))
function longtest() {
  var o=[],i;
  for (i=1;i<1e6;i++)
    b=i.toString(2),v=f(b),v!=i?o.push(b+' '+v):0;
  O.innerHTML=o.join`\n`
}
```
```
Click to run the long test <button onclick="longtest()">go</button>
<pre id=O></pre>
```
[Answer]
# Mathematica, ~~62~~ ~~59~~ ~~55~~ 48 bytes
Saved 7 bytes thanks to Martin B√ºttner.
```
#//.a_/;Max[b=IntegerDigits@a]<2:>Fold[#+##&,b]&
```
[Answer]
# Javascript (ES7) 87 80 78 77 74 bytes
Snippet demo for supporting browsers (currently only Firefox nightly supports the exponential operator)
```
f=x=>[...x].reverse(i=y=j=0).map(z=>(j|=z,y+=z*2**i++))&&j<2&y>1?f(y+[]):x
```
```
<input type="text" id="x" value="1111110011"><button onclick="o.innerHTML=f(x.value)">Run</button><div id="o"></div>
```
```
f=x=>
[...x].reverse(i=y=j=0) // reverse string as array, initialize vars
.map(z=>( // iterate over the all chatacters
    j|=z, // keep track of whether a digit higher than 1 is encountered
    y+=z*2**i++ // build decimal result from binary
))&&
j<2&y>1? // if we encountered only 1's and 0's and result > 1
    f(y+[]) // then call recursively and cast to a string
    :x // else return x
```
# Javascript (ES6) 81 bytes
Snippet demo for supporting browsers
```
f=x=>[...x].reverse(i=y=j=0).map(z=>y+=z*Math.pow(2,i++,j|=z))&&j<2&y>1?f(y+[]):x
```
```
<input type="text" id="x" value="1111110011"><button onclick="o.innerHTML=f(x.value)">Run</button><div id="o"></div>
```
[Answer]
# ùîºùïäùïÑùïöùïü, 37 chars / 54 bytes
```
‚Ü∫;√Ø>1‚Öã(‚¨Ø+√Ø)ƒâ/^[01]+$‚åø);)√Ø=+('·∂Ä'+√Ø);√¥√Ø
```
`[Try it here (Firefox only).](http://molarmanful.github.io/ESMin/interpreter.html?eval=true&input=1010&code=%E2%86%BA%3B%C3%AF%3E1%E2%85%8B%28%E2%AC%AF%2B%C3%AF%29%C4%89%2F%5E%5B01%5D%2B%24%E2%8C%BF%29%3B%29%C3%AF%3D%2B%28%27%E1%B6%80%27%2B%C3%AF%29%3B%C3%B4%C3%AF)`
Not sure if the `+` operator counts as a built-in for binary conversion...
[Answer]
## [Perl 6](http://perl6.org), 67 bytes
```
get,{$_=0;for $^a.comb {$_+<=1;$_+=$^b};$_}...1|/<-[01]>/;say $_//1
```
[Answer]
# PHP, 210 204 bytes
It's my first time posting here, so hope you guys will like it ! Even if it's obviously not the best way to write it, I'm still glad to show it off here !
## The Code
```
<?function j($a){$c=0;if($a==1){return 1;}else{if(preg_match("#^[01]+$#",$a)){$b=strlen($a);$a=str_split($a);foreach($a as$d){$c+=($d==0?0:2**($b-1));$b--;}return j($c);}else{return$a;}}}echo j($_GET[0]);
```
I've made a recursive function "j" that will first check if the input is equal to 1. If so, the function returns 1 as expected, else it'll split the number in an array to calculate the decimal value, but only if the number is a binary one. If it's not, it'll return the number as is.
## Ungolfed code
```
<?
function j($a) {
  $c = 0;
  if ($a == 1) {
    return 1;
  }
  else {
    if (preg_match("#^[01]+$#", $a) {
      $b = strlen($a);
      $a = str_split($a);
      foreach ($a as $d) {
        $c += ($d == 0 ? 0 : 2 ** ($b - 1));
        $b--;
      }
      return j($c);
    }
    else {
      return $a;
    }
  }
}
echo j($_GET[0]);
```
I've used a "foreach" statement instead of my initial "for" one, allowing me a gain of 6 bytes but I'm pretty sure there's a lot more to do.
[Answer]
# PHP, ~~114~~ 112 bytes
also works for `0`. Run with `-r`.
```
for($n=$argv[1];count_chars($s="$n",3)<2&$s>1;)for($i=$n=0;""<$c=$s[$i++];)$n+=$n+$c;echo$s;
```
`count_chars($s,3)` returns a string containing all characters from the string (like `array_unique` does for arrays). For binary numbers, this will be `0`, `1` or `01`. For other numbers, this will contain a digit larger than `1`, so `<2`will return true only for binary numbers.
`&$s>1` is needed for the special case `1`.
The rest is straight forward: Loop through the bits with shifting the value and adding the current bit, finally copy the number (cast to string) to $s for the outer loop test.
[Answer]
## CoffeeScript, ~~92~~ 89 bytes
```
f=(x)->x>1&/^[01]+$/.test(x)&&f(''+x.split('').reverse().reduce ((p,v,i)->p+v*2**i),0)||x
```
## JavaScript (ES6), ~~105~~ ~~101~~ 90 bytes
```
f=y=>y>1&/^[01]+$/.test(y)?f(''+[...y].reverse().reduce(((p,v,i)=>p+v*Math.pow(2,i)),0)):y
```
## Demo
Only works in ES6-compliant browsers such as Firefox and Microsoft Edge
```
f=y=>y>1&/^[01]+$/.test(y)?f(''+[...y].reverse().reduce(((p,v,i)=>p+v*Math.pow(2,i)),0)):y
// Snippet stuff
$(`form`).submit((e) => {
  document.getElementById(`y`).textContent = f(document.getElementById(`x`).value);
  e.preventDefault()
})
```
```
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form>
  <label>Input:
    <input pattern=^[01]+$ required id=x>
  </label>
  <button type=submit>Go</button>
  <p>Output:
    <output id=y></output>
  </p>
</form>
```
[Answer]
# Scala, 128 bytes
```
def b(s:String):String=if(s.matches("[10]{2,}"))b(""+s.reverse.zipWithIndex.collect{case('1',i)=>Math.pow(2,i)}.sum.toInt)else s
```
[Answer]
## Matlab (115)
```
@(a)num2str(sum((fliplr(a)-48).*arrayfun(@(x)2^x,0:nnz(a)-1)));a=ans(input('','s'));while(find(a<50))a=ans(a);end,a
```
---
* The anonymous function is number type conversion (`bin2dec`)
]