0
\$\begingroup\$

Definition

Given some number, x calculate the smallest number i such that 2ix. 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.

asked Dec 10, 2021 at 1:07
\$\endgroup\$
12
  • \$\begingroup\$ This question is off-topic because We use C# or any language? I recommend to post Sandbox to iron out bugs. \$\endgroup\$ Commented Dec 10, 2021 at 1:14
  • \$\begingroup\$ any language. There are no bugs in my example?? \$\endgroup\$ Commented 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\$ Commented 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\$ Commented 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\$ Commented Dec 10, 2021 at 1:34

4 Answers 4

4
\$\begingroup\$

Vyxal, 5 bytes

≬E?≥ṅ

Try it Online!

Quite literally returns the first number where 2**n >= input

answered Dec 10, 2021 at 1:17
\$\endgroup\$
3
  • \$\begingroup\$ I just got a tie in 05ab1e at 5 bytes: 2.nî \$\endgroup\$ Commented 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 your 2.nî is 4 bytes, not 5.. Did you accidentally counted the bytes in UTF-8 instead of 05AB1E encoding? \$\endgroup\$ Commented Dec 10, 2021 at 7:23
  • \$\begingroup\$ Yea I counted in UTF-8. What is the normal way to count the bytes? \$\endgroup\$ Commented Dec 10, 2021 at 18:29
0
\$\begingroup\$

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.

answered Dec 10, 2021 at 1:23
\$\endgroup\$
0
\$\begingroup\$

Pari/GP, 23 bytes

n->if(n,#binary(n-1),0)

Try it online!

answered Dec 10, 2021 at 1:23
\$\endgroup\$
0
\$\begingroup\$

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.

answered Dec 10, 2021 at 1:38
\$\endgroup\$

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.