All about basic binary

Please excuse the punny title.

This is a question is inspired by A Curious Property of 82000. In it, the author points out that the number 82000 is binary in base 2, 3, 4, and 5. The post then poses the question "is there a number that is binary in bases 2, 3, 4, 5, and 6"? (For those curious, I've checked values up to 10^1,000,000 and so far the answer is no.)

This got me thinking: given a number, what bases is it binary in?

Our curious number, 82000, is actually binary in six bases:

Base 2 = 10100000001010000
Base 3 = 11011111001
Base 4 = 110001100
Base 5 = 10111000
Base 81999 = 11
Base 82000 = 10

Not all numbers will have binary bases that are sequential. Consider the number 83521. It's binary in bases 2, 17, 289, 83520, and 83521.

Your challenge is to determine and display which bases a number is binary in.

Rules

• A number is considered "binary" in a given base if its representation in that base consists of only zeroes and ones. 110110 is a binary value, while 12345 is not, A380F is definitely not.
• Your number will be provided on standard input. It will be an integer value between 2 and 2^32-1 inclusive and will be provided in base-10 format.
• In ascending order, display each base greater than one that the number is binary in. Each base should be on its own line. If you include the binary value in that base (see the bonus scoring below), separate the base and the binary value with a space. Only output to standard out will be judged, standard error and other sources will be ignored.

Scoring

Your score is your program's size in bytes. The lower the score, the better.

Bonus:
If your program also outputs the binary values in the found bases, multiply your score by 0.75
Your displayed binary value should have no extra punctuation, no extraneous zeroes, no decimal point, just zeroes and ones.

Examples

Input:

82000

2 10100000001010000
3 11011111001
4 110001100
5 10111000
81999 11
82000 10

Input:

1234321

Output (no bonus):

2
1111
1234320
1234321
• Can the input end with a newline? – LegionMammal978 Jun 2 '15 at 19:26
• @LegionMammal978 - Uhhh... sure? My intent was that you should be able to get the input number with a simple fgets, readline, or something similar. – Mr. Llama Jun 2 '15 at 19:29
• In general, n is always at least binary in bases 1 (not counted), 2, n-1, and n. – mbomb007 Jun 2 '15 at 19:44
• When you say, "your number will be provided on standard input," do you mean STDIN only, or can we alternatively accept the number as a function argument as is standard for the site? – Alex A. Jun 2 '15 at 20:20
• Should the binary representation (in the bonus part) have a certain format? Particularly would [1, 0, 1, 1, 0] be o.k., or does the numbers have to be joined like 10110? – Jakube Jun 2 '15 at 21:08

Pyth, 14 13

jbf!-jQTU2tSQ

Thanks to Jakube for pointing out the new S function.

Try it here.

The online version is too slow to do 1234321. This simply converts the input to each base from 2 to itself and discards the results that contain values other than 0 and 1.

Explanation:

: Q=eval(input) (implicit)
jb                         : join on newlines the list...
f!                       : filter away nonempty values (impliticly named T)
-jQTU2                 : sewtise difference of number converted to base and range(2)
jQT                   : convert Q to base T
U2                 : range(2)
tSQ              : over the range [2 Q+1)

In addition, this is a (not well golfed now well golfed, again thanks to Jakube) bonus version (20 * .75 = 15):

VQI!-JjQK+2NU2pdKjkJ

Try it here

• Pyth just got updated. So you can link to actual solutions. – Jakube Jun 3 '15 at 8:47
• And here's a 20 * 0.75 = 15 solution: VQI!-JjQK+2NU2pdKjkJ Sometimes functional programming is not the best approach. – Jakube Jun 3 '15 at 8:48

Julia, 72 70 bytes

It's actually longer with the bonus, so no bonus here.

This reads a line from STDIN, converts it to an integer, and prints the result. Despite being a brute force method, the input 1234321 took less than 1 second for me.

Ungolfed + explanation:

# Read n from STDIN and convert to integer

# For every potential base from 2 to n
for j = 2:n
# If all digits of n in base j are 0 or 1
if all(i -> i ∈ 0:1, digits(n, j))
# Print the base on its own line
println(j)
end
end

Examples:

julia> n=int(readline());for j=2:n all(i->i∈0:1,digits(n,j))&&println(j)end
1234321
2
1111
1234320
1234321

julia> n=int(readline());for j=2:n all(i->i∈0:1,digits(n,j))&&println(j)end
82000
2
3
4
5
81999
82000

NOTE: If input can be taken as a function argument rather than from STDIN (awaiting confirmation from the OP), the solution is 55 bytes.

CJam, 20 bytes (or 27 bytes * 0.75 = 20.25)

Here is the no bonus version, 20 bytes:

ri:X,2f+{X\b2,-!},N*

Try this here.

Just for fun, here is the bonus version, 27 bytes:

ri:X{X\)b2,-!},1>{)SX2$bN}/ Try it online here • Sure. Once I am done a bit of golfing. – Optimizer Jun 2 '15 at 19:37 • ri_,f{2+S@2$bN}4/{2=2,-!}, (19.5 bytes) – Dennis Jun 2 '15 at 20:15

Mathematica, 59 bytes

Print/@Select[1+Range[n=Input[]],Max@IntegerDigits[n,#]<2&]

Ugh... IntegerDigits D:

There isn't really much to explain about the code... 12 bytes are wasted by the requirement for using STDIN and STDOUT.

I don't think I can claim the bonus. The best I've got is 84 bytes (which yields a score over 60):

Print@@@Select[{b=#+1," ",##&@@n~IntegerDigits~b}&/@Range[n=Input[]],Max@##3<2&@@#&]

Python 2, 88 86 80

Fairly straightforward, no bonus. Python is nice and lenient with global variables.

N=input();g=lambda n:n<1or(n%b<2)*g(n/b)
for b in range(2,N+1):
if g(N):print b

Best I've managed to get for the bonus is 118*.75 = 87.75:

N=input();g=lambda n:0**n*" "or" "*(n%b<2)and(g(n/b)+n%b)*(g(n/b)>'')
for b in range(2,N+1):
if g(N):printb+g(N)
• Nice solution, beat me to it with much shorter code. – Kade Jun 2 '15 at 20:17
• It would be shorter to just do g(N) instead of n=N. – feersum Jun 2 '15 at 20:40
• @feersum Oh yeah (it used to be g(N,b) so the comma made the two equal), but what do you mean I wouldn't need a variable for N? – KSab Jun 2 '15 at 20:44
• @KSab I deleted that second part; never mind it. – feersum Jun 2 '15 at 20:45
• I may be wrong but couldn't you get the bonus by just changing g(n/b) to (g(n/b)+'n%b') where ' represents a backtick? – feersum Jun 2 '15 at 20:46

Python 2, 90 * 0.75 = 67.5

n=input();b=1
while b<n:
b+=1;s="";c=k=n
while k:s=k%b+s;c*=k%b<2;k/=b
if c:print b,s

Pretty straightforward iterative approach.

Without the bonus, this is 73 bytes:

n=input();b=1
while b<n:
b+=1;c=k=n
while k:c*=k%b<2;k/=b
if c:print b

SQL (PostgreSQL), 247.5255 230.25 (307 * .75)

Since SQL is known to be wonderful at these sorts of challenges, I thought I better put one together :) The bonus was really worthwhile for this one.
It should comply with spec, but I have no easy way to test the COPY I FROM STDIN.
Edit Fixed order. Changed the way column R is handled to use an array.

CREATE TABLE IF NOT EXISTS I(I INT);TRUNCATE TABLE I;COPY I FROM STDIN;WITH RECURSIVE R AS(SELECT n,I/n I,ARRAY[I%n] R FROM generate_series(2,(SELECT I FROM I))g(n),(SELECT I FROM I)I(I)UNION ALL SELECT n,I/n,I%n||R FROM R WHERE I>0)SELECT n||' '||array_to_string(R,'')FROM R WHERE 2>ALL(R)and i=0ORDER BY n

As a test I just used straight inserts into the I table. Test run expanded and commented.

-- Create the table to accept the input from the copy command
CREATE TABLE IF NOT EXISTS I(I INT);
-- Make sure that it is empty
TRUNCATE TABLE I;
-- Popoulate it with a value from STDIN
--COPY I FROM STDIN;
INSERT INTO I VALUES(82000); -- Testing
--Using a recursive CTE query
WITH RECURSIVE R AS (
-- Recursive anchor
SELECT n,                -- base for the row
I/n I,                -- integer division
ARRAY[I%n] R   -- put mod value in an array
FROM generate_series(2,(SELECT I FROM I))g(n), -- series for the bases
(SELECT I FROM I)I(I) -- Cross joined with I,  saves a few characters
UNION ALL
-- for each row from r recursively repeat the division and mod until i is 0
SELECT n,
I/n,
I%n||R -- Append mod to beginning of the array
FROM R WHERE I>0
)
-- return from r where i=0 and r has 1's and 0's only
SELECT n||' '||array_to_string(R,'')
FROM R
WHERE 2 > ALL(R)and i=0
ORDER BY n -- Ensure correct order

2 10100000001010000
3 11011111001
4 110001100
5 10111000
81999 11
82000 10

• So close! The output bases needs to be in ascending order. +1 for using an unconventional language though. – Mr. Llama Jun 3 '15 at 21:13
• @Mr.Llama fixed it with an order by. Now to see if I can get those characters back – MickyT Jun 3 '15 at 21:23

Haskell 109*0.75 = 81.75 bytes

0#x=[]
n#x=nmodx:div n x#x
f n=[show x++' ':(n#x>>=show)|x<-[2..n+1],all(<2)$n#x] p=interact$unlines.f.read

Usage example (note: binary values are lsb first):

p 82000

2 00001010000000101
3 10011111011
4 001100011
5 00011101
81999 11
82000 01

Without input/output restrictions, i.e input via function argument, output in native format via REPL):

Haskell, 67*0.75 = 50.25 bytes

0#x=[]
n#x=nmodx:div n x#x

Try it online!