Definition
Given some number, x calculate the smallest number i such that 2i≥x. This is not a duplicate of this question. It is slightly different in that an input of 16 should yield an output of 4 whereas 16 returns 5 in the linked question.
Input
Input will be an unsigned 32-bit integer.
Output
Output will be an unsigned integer (8-bit, 16-bit, 32-bit etc. are all ok)
Test Cases
Input | Output |
---|---|
0 | 0 |
1 | 0 |
2 | 1 |
15 | 4 |
16 | 4 |
65536 | 16 |
Example (C# 51 bytes)
uint A(uint x) => (uint)Math.Ceiling(Math.Log2(x));
This is code-golf, so the shortest answer in bytes wins! Standard loopholes and rules apply. Tie goes to first poster.
-
\$\begingroup\$ This question is off-topic because We use C# or any language? I recommend to post Sandbox to iron out bugs. \$\endgroup\$Fmbalbuena– Fmbalbuena2021年12月10日 01:14:57 +00:00Commented Dec 10, 2021 at 1:14
-
\$\begingroup\$ any language. There are no bugs in my example?? \$\endgroup\$Nigel– Nigel2021年12月10日 01:15:43 +00:00Commented Dec 10, 2021 at 1:15
-
\$\begingroup\$ Output will be an unsigned integer (8-bit, 16-bit, 32-bit etc. are all ok). while the Input says Input will be an unsigned 32-bit integer. \$\endgroup\$Fmbalbuena– Fmbalbuena2021年12月10日 01:17:24 +00:00Commented Dec 10, 2021 at 1:17
-
1\$\begingroup\$ On this site, duplicate-ness does not depend on exact equality of tasks. Instead, if many of the existing answers on challenge A can be trivially modified to get answers on challenge B, B is considered a dupe of A. In this case, A is the linked challenge, B is yours, and the trivial modification is subtracting the input by 1. Your unit test argument simply doesn't work on this site. Also, the second link I gave you is now open to all languages. \$\endgroup\$Bubbler– Bubbler2021年12月10日 01:28:42 +00:00Commented Dec 10, 2021 at 1:28
-
1\$\begingroup\$ @NigelBess Sorry, we can't really make an exception for this challenge, no matter how good it is. People can always have fun answering the linked question. Perhaps if you post a draft in the Sandbox, people could suggest how to modify your challenge to make it more interesting than the one it's a duplicate of. \$\endgroup\$user– user2021年12月10日 01:34:44 +00:00Commented Dec 10, 2021 at 1:34
4 Answers 4
-
\$\begingroup\$ I just got a tie in 05ab1e at 5 bytes:
2.nî
\$\endgroup\$Nigel– Nigel2021年12月10日 01:44:54 +00:00Commented Dec 10, 2021 at 1:44 -
\$\begingroup\$ @NigelBess An equal-bytes 05AB1E answer similar as the Vyxal program could be
∞.Δo‹
. :) Although, as noted by Bubbler, simply<bg
would be a trivial 3-byter based on the challenge that was marked as duplicate. EDIT: And your2.nî
is 4 bytes, not 5.. Did you accidentally counted the bytes in UTF-8 instead of 05AB1E encoding? \$\endgroup\$Kevin Cruijssen– Kevin Cruijssen2021年12月10日 07:23:54 +00:00Commented Dec 10, 2021 at 7:23 -
\$\begingroup\$ Yea I counted in UTF-8. What is the normal way to count the bytes? \$\endgroup\$Nigel– Nigel2021年12月10日 18:29:47 +00:00Commented Dec 10, 2021 at 18:29
Charcoal, 10 bytes
IL↨⌈⟦0⊖N⟧2
Try it online! Link is to verbose version of code. Explanation:
N Input as a number
⊖ Decremented
0 Literal `0`
⟦ ⟧ List of the above
⌈ Maximum
↨ Convert to base
2 Literal `2`
L Length
I Cast to string
Implicitly print
Charcoal converts 0
to []
so giving the correct log for an input of 1
, but 0
as input needs to be special-cased.
Retina 0.8.2, 26 bytes
.+
$*
^((1+)(?=2円))*1*
$#1
Try it online! Link includes test cases. Explanation:
.+
$*
Convert the input to unary.
^((1+)(?=2円))*1*
Divide by two as many times as possible, rounding up each time, until 1
is reached. (The leading ^
and trailing *
are needed to handle the edge case of zero input.)
$#1
Output the number of rounded divisions made.