# Calculate $\lfloor n \log_2(n) \rfloor$, exactly

Given an integer $$\ n \ge 2 \$$, you need to calculate $$\ \lfloor n \log_2(n) \rfloor \$$, assuming all integers in your language are unbounded.

However, you may not ignore floating-point errors - for example, in python lambda n:int(n*math.log2(n)) is an invalid solution, because for example for n=10**15, int(n*math.log2(n)) is 49828921423310432, while the actual answer is 49828921423310435.

## Test cases

2 -> 2
3 -> 4
4 -> 8
5 -> 11
6 -> 15
7 -> 19
8 -> 24
9 -> 28
10 -> 33
100 -> 664
1000 -> 9965
10000 -> 132877


10 brownie points for beating my 4 byte 05AB1E answer.

This is code golf, so the shortest answer wins. Good luck!

• This is OEIS A326299. May 24 at 17:56

# Python 3, 25 bytes

lambda n:len(bin(n**n))-3


Try it online!

If the answer is $$\x\$$, then $$\x+1 > n\log{n} \ge x\$$ holds true, which means $$\2^{x+1} > n^n \ge 2^x\$$. So we can simply count the number of bits in the binary representation of $$\n^n\$$.

• Have you tested it for big numbers? Because I tried 10^7 (not on the TIO, on the PC), which isn't even that big and the execution took like 2-3 minutes. Does it still work for 10^100 mentioned under other of answers? May 24 at 15:50
• @elementiro This is code golf, so as long as it works in theory the answer is valid. If it fails because of timing out or out-of-memory errors, that's perfectly fine. May 24 at 15:57

# Vyxal, 4 bytes

ebL‹


Try it Online!

# Jelly, 5 bytes

*BL’


Try it online!

Ditto.

# Jelly, 4 bytes

*ḃ2L


A monadic Link accepting an integer, $$\n>1\$$, that yields $$\ \lfloor n \log_2(n) \rfloor \$$.

Try it online!

### How?

As noted by Manish Kundu the answer is one less than the number of binary digits of $$\n^n\$$.

This can be achieved in Jelly in five bytes in a few ways one of which is: *b2L’.

Note, however, that $$\n^n=2^x-1\$$ has no integer solutions except $$\n=x=1\$$ (see Wolfram Alpha*) and that the bijective base two representation of a number always has one less digit than the standard base two representation except when the number is of the form $$\2^k-1\$$ (when both representations are identical, a string of $$\k\$$ ones).

Since $$\n=1\$$ is invalid input we can, therefore, count the number of digits in the bijective base two representation of $$\n^n\$$.

*ḃ2L - Link: integer, n
*    - (n) exponentiate (n) -> n^n
2  - two
ḃ   - (n^n) in bijective base (2)
L - length


* if someone has a proof, I'll happily put it here!

# Julia, 18 24 bytes

n->ndigits(n^n,base=2)-1


Try it online!

n->floor(log2(n)n)


Try it online!

input needs to be a BigInt to avoid floating point errors.

Technically invalid because you need to augment the precision for very big numbers (with setprecision(2048) for example for 10^100)

• The output is wrong for BigInt(10)^100. May 24 at 14:42
• what is the right answer ? May 24 at 14:46
• 3321928094887362347870319429489390175864831393024580612054756395815934776608625215850139743359370155099 May 24 at 14:51
• It will work by increasing the precision, but you can always find a bigger number that won't work Try it online! May 24 at 15:07
• is it ok if I restrict the ouput to Int64 like this ? Try it online! May 24 at 15:09

# JavaScript (Node.js), 30 bytes

Same approach as Manish Kundu.

Expects a BigInt.

n=>(g=n=>n?g(n/2n)+1:-1)(n**n)


Try it online!

• Crashes on 10000 :(
– user7467
May 24 at 18:33
• n=>(n**n).toString(2).length-1 seems to work for 10000 for the same byte count.
– Neil
May 24 at 18:47
• g(n/2n) <- Another wtf js behavior here
– tsh
May 25 at 1:49

$_=int$_*(1*$_)->blog(2)  Try it online! # Ruby, 20 bytes ->n{/.$/=~"%b"%n**n}


Try it online!

Get the length of the binary string representation of n^n. Basically uses the same formula as Manish Kundu and everybody else after him.

Using bit_width or digits(2).size would make it longer.

# Charcoal, 10 8 bytes

Ｉ⊖Ｌ↨Ｘθθ²


Try it online! Link is to verbose version of code. Explanation: Uses the same identity $$\ \lfloor n \log_2 n \rfloor = \lfloor \log_2 n^n \rfloor \$$ as everyone else. (Exponentially slow for large n of course, so stick to 4 digit inputs for testing on TIO.) Edit: Saved 2 bytes thanks to @CommandMaster.

• Since unary output is allowed by default, can't it be 9 bytes? May 25 at 4:25
• ⊖Ｌ↨Ｘθθ² seems to work for 7 bytes? May 25 at 4:35
• @CommandMaster Huh, I didn't realise that Power casts its arguments to number first, thanks. I don't think unary output is reasonable though (even for Retina I prefer to use decimal I/O when I can).
– Neil
May 25 at 8:54

# 05AB1E (legacy), 4 bytes

mbg<


Try it online!

• Why is this legacy? May 24 at 15:58
• @Makonede It's an old version of the language May 24 at 16:16
• @LuisMendo I know. That's not my question. My question is why use legacy when you can use the rewrite? May 24 at 16:25
• @makonede it doesn't quite matter May 24 at 16:25

# Pyth, 5 bytes

tl.B^


Test suite

A near literal translation of Manish Kundu's Python 3 answer.

# Java, 36 bytes

n->n.pow(n.intValue()).bitLength()-1


Accepts a BigInteger as input.

Try it online!

# Wolfram Language (Mathematica), 17 15 bytes (11 characters)

⌊#*Log2@#⌋&


-2 thanks to ZaMoC!

Try it online!

• How can you be sure this won't FPE eventually? May 24 at 18:04
• In Mathematica these calculations are exact, no floating-point approximations involved and therefore no overflows and limitations. Just try f[10^10^6] for fun! May 24 at 18:08
• 15 bytes May 24 at 20:53

# C (gcc), 52 $$\\cdots\$$35 23 bytes

Saved 8 bytes thanks to tsh!!!

f(n){n=logb(pow(n,n));}


Try it online!

Uses the formula from Manish Kundu's Python answer.

• f(n){n=logb(pow(n,n));}
– tsh
May 25 at 5:26
• @tsh Nice one - thanks! :D May 25 at 11:49
• Isn't pow a floating-point function? May 26 at 3:37
• @CommandMaster IEEE754 floating point number have more bits for fraction than exponent. And it use binary as its representation. So it always yield correct result as long as pow(n,n) not exceed the limit of the floating point type (not yield +inf). And the same algorithm may be extend to larger IEEE754 floating types such as binary256 (if the compiler support it). The question disallow floating point errors, but not floating point functions.
– tsh
May 26 at 7:27

# MATLAB/Octave, 30 bytes

@(x)floor(sym(x)*log2(sym(x)))


Try it online! Anonymous function. The result is symbolic number but can be easily cast to any other type. This solution works as long as the upper bound of int64 allows it.

Technically, much better solution is @(x)floor(x*log2((x))) (which is 20 bytes btw) with symbolic input. Since the question says input is integer I couldn't use it though. Because our input is unbounded it works for much much bigger numbers (I managed to calculate 101 000 000 but the calculations took a while and the output in console was truncated, for too big numbers throws an error though). I encourage anyone to check the result for 10300 if their method is capable of working for such big inputs:

f(sym(10^300)) =

996578428466208704361095828846817052759449417907374183616426918744780432982587564755041923007811046529897211513075075548047229095279058066482590831899596618155585405797251670592905282757007467500082430012203570937256212532914759940262053756896742020749233906529817764982497756734670715067992925218241378
`

...did I mention unlike some of the other answers this takes only ~0.05s for MATLAB to calculate even for that big inputs?