| Bytes | Lang | Time | Link |
|---|---|---|---|
| 010 | Japt | 241129T181130Z | Shaggy |
| 011 | Uiua | 241128T181204Z | noodle p |
| 047 | Perl 5 p | 241127T181334Z | Kjetil S |
| 034 | Python 3.8 prerelease | 241127T153829Z | squarero |
| 027 | Haskell | 241127T185037Z | colossus |
| 014 | x86_64 machine code | 241127T191131Z | anatolyg |
| 033 | Python | 241128T110600Z | Albert.L |
| 013 | MathGolf | 241128T095000Z | Kevin Cr |
| 007 | 05AB1E | 241128T081132Z | Kevin Cr |
| 037 | Python 2 | 241127T225233Z | Lucenapo |
| 007 | Vyxal | 241127T202227Z | emanresu |
| 027 | R | 241127T184834Z | pajonk |
| 026 | APL+WIN | 241127T174604Z | Graham |
| 015 | Charcoal | 241127T172641Z | Neil |
| 063 | Retina | 241127T171210Z | Neil |
| 044 | Google Sheets | 241127T151536Z | doubleun |
| 029 | JavaScript ES11 | 241127T153219Z | Arnauld |
Japt, 10 bytes
Inspired by Kevin's MathGolf solution.
@<G²pX}a!²
@<G²pX}a!² :Implicit input of integer U
@ :Function taking an integer X as argument
< : U is less than
G : 16
² : Squared
pX : Raised to the power of X
} :End function
a :Apply the following method to the non-negative integers,
: returning the first result that returns true
: when passed through that function
!² :Power of 2
Uiua, 11 bytes
⍜ₙ⌈2⌈ₙ256+1
Try it: Uiua pad
⌈ₙ256+1 -- The number of bits needed to represent the number is the logarithm with base 256 of 1 + the input, rounded up.
⍜ₙ⌈2 -- "under logarithm ceiling 2", so \$2^{\lceil log_2 n \rceil}\$ (where \$ \lceil x \rceil \$ means x rounded up), yielding the smallest power of 2 greater than n.
Perl 5 -p, 47 bytes
$_=$_>9e10?8:$_?1<<1+log(log()/log 256)/log 2:1
The 10 bytes of $_>9e10?8: could have been dropped if log had more floating point accuracy I think. And $_=$_>=1<<32?8:$_>65535?4:$_>255?2:1 is 36 bytes Try it online!
Inspired by the Python from @Lucenaposition: $_=[1,2,4,4]->[log($_|1)/log 256]||8 Try it online! 36 bytes.
Python 3.8 (pre-release), 59 47 38 34 bytes
-12 bytes by having 5 more minutes to codegolf.
-9 bytes by stealing Arnauld's idea.
-4 bytes from Arnauld.
f=lambda n,k=8:n>>k<1or 2*f(n,2*k)
Haskell, 32 29 27 bytes
f n=until(\b->n<256^b)(*2)1
EDIT: -3 bytes because I realized I could drop the -1 if I switched to using <
EDIT 2: -2 bytes thanks to @xnor's suggestion to use 256^b.
x86_64 machine code, 14 bytes
B0 FF mov al,0FFh
48 0F BD C0 bsr rax,rax
0F BD C8 bsr ecx,eax
B0 40 mov al,40h
D2 C0 rol al,cl
C3 ret
- If x < 223, its type is
1, or 20 - If 223 ≤ x < 224, its type is
2, or 21 - If 224 ≤ x < 225, its type is
4, or 22 - If 225 ≤ x < 226, its type is
8, or 23
To apply this, we can calculate y = log2(log2(x)), and then z = 2 ** y, with suitable offsets and rounding.
To ensure good behavior near 0, the code first ensures x ≥ 255 (I think greater than 3 should be enough; used 255 for safety).
Python, 33 bytes
f=lambda n:n<256or 8-6*f(n**.5)%8
Returns True for 1.
How?
Same idea as @xnor's comment here. The tricky bit was fixing the fp issue cheaply.
MathGolf, 13 bytes
8421Γgæ♠▬k>~Þ
Explanation:
8421 # Push 8,4,2,1 separated to the stack
Γ # Wrap all four into a list: [8,4,2,1]
g # Filter this list by,
æ # using four character as inner code-block:
♠ # Push 256
▬ # Take 256 to the power the current value
k # Push the input
> # Check whether the 256 to the value is larger than the input
~ # After the filter: dump all remaining digits to the stack
Þ # Only keep the last one
# (after which the entire stack is output implicitly as result)
05AB1E, 7 bytes
₁вg.²îo
Try it online or verify all test cases.
Explanation:
₁в # Convert the (implicit) input-integer to a base-256 list
g # Pop and push its length
.² # Take the logarithm_2 of that
î # Ceil it
o # Take 2 to the power that
# (which is output implicitly as result)
Python 2, 37 bytes
lambda n:'12448888'[len(bin(n))-3>>3]
Outputs a string (if it is allowed).
Python 2, 45 bytes
lambda n:0x88884421>>((len(bin(n))-3)/8*4)&15
Outputs a number.
Vyxal, 7 bytes
₈τL∆l⌈E
L # Length
τ # when converting to base
₈ # 256
# (i.e. number of bytes required)
E # 2 to the power of
∆l # log2 of that
⌈ # rounded up
APL+WIN, 26 bytes
Prompts for integer
¯1↑((2⍟1⌈⎕)≥0,2*3+⍳3)/2*⍳4
Charcoal, 15 bytes
IX²L↨÷⊖L⍘N²¦⁸¦²
Try it online! Link is to verbose version of code. Explanation:
N Input as an integer
⍘ Converted to string base
² Literal integer `2`
L Length
⊖ Decremented
÷ Integer divided by
⁸ Literal integer `8`
↨ Converted to base
² Literal integer `2`
L Take the length
² Literal integer `2`
X Raised to that power
I Cast to string
Implicitly print
The first base conversion converts 0 to "0" while the second one converts it to []. This makes a difference when taking the length.
Retina, 63 bytes
$
¶$`¶256¶65536¶4294967296
N`¶.+
Lm`^(.+)¶(.+¶)*\1$
C`¶
T`34`48
Try it online! Link includes test cases. Unfortunately I had to use Retina 1 for this as Retina 0.8.2 uses 32-bit arithmetic for sorting. Explanation:
$
¶$`¶256¶65536¶4294967296
Append a copy of the input and 256, its square and squared square.
N`¶.+
Sort the copy among the powers.
Lm`^(.+)¶(.+¶)*\1$
Find where it ended up.
C`¶
Count how many squares were needed, plus one.
T`34`48
Translate into a number of bytes.
Google Sheets, 44 bytes
=ifs(A1<2^8,1,A1<2^16,2,A1<2^32,4,A1<2^64,8)

Google Sheets uses IEEE 754 double-precision with a significand of 53 bits for pretty much everything, so cannot go all the way up to 64-bit unsigned.
The output format rules specified in the question are quite rigid. This alternative version outputs an array where the first true value marks the minimum bytes required by the unsigned integer (23 bytes):
=sort(A2<2^2^{3,4,5,6})
