J, 19 bytes
Monadic verb taking the number on the right and spitting out the container size. There are a couple of equivalent ways of writing it so I've included both.
2^2(>.+1=>.)@^.#@#:
2^s+1=s=.2>.@^.#@#:
Explained by explosion:
2^2(>.+1=>.)@^.#@#: NB. takes one argument on the right...
#: NB. write it in binary
#@ NB. length (i.e. how many bits did that take?)
2 ^. NB. log base 2 of that
(>. )@ NB. ceiling
+1=>. NB. +1 if needed (since no container is two bits wide)
2^ NB. base 2 exponential
What's cool is we see two different ways of taking log base 2 in J. The first is the obvious 2^.
, which is a numerical logarithm. The second is #@#:
, which can be read as "length of base-2 representation". This is almost equivalent to one-plus-floor-of-log-base-2, except that #:0
is the one-element list 0
, which is exactly what we want. This beats 1+2<.@^.1&>.
by 8 bytes.
In use at the REPL:
f =: 2^2(>.+1=>.)@^.#@#:
f 131313
32
f 34359750709
64
(,.f"0) 0 1 2 15 16 123 260
0 1
1 1
2 4
15 4
16 8
123 8
260 16
Old, overly clever 20 byte solution.
2&^.(>.+1=>.&.)@#@#: NB. takes one argument on the right...
#@#: NB. how many bits
2&^. NB. log base 2 of that
>. NB. ceiling
+1=>. NB. +1 if needed (since no container is two bits wide)
( &.) NB. undo log base 2
10This would be considerably easier if
2
was an output too... – ETHproductions – 2016-12-28T21:15:37.1031@ETHproductions It would but alas, it isn't (it took me far to long to write an algorithm that did it) – Blue – 2016-12-28T21:16:19.113
I wish I understood the problem. ... wait, all it wants is the amount of bits needed to contain the number, rounded to the next fundamental structure? – z0rberg's – 2016-12-29T10:48:39.647
@z0rberg's basically find the smallest container that the number will fit in out of the ones given. – Blue – 2016-12-29T10:50:45.450
2Thanks! I realized it when I wrote the comment and edited it too late. I guess I need a rubber duck to talk to... – z0rberg's – 2016-12-29T10:53:04.603
2@Daniel the answers here take a completely different approach to the other question. When I say 'inspired by' it does not mean 'duplicate of'. No answers could be trivially modified to be valid for this question – Blue – 2016-12-29T18:01:22.210
AFAIK if it had all integers, the answer would be ceil(log2(n)) – Matthew Roh – 2017-02-27T13:37:34.210