Here is a weird trick I learned some time ago. Suppose we want to compute $n=\log_b a$ and we know $n$, $a$ and $b>1$ are positive integers. It turns out that

So for choices of $k$ where $% $, this wacky expression evaluates to $n$:

a**k/(b**k-1)%(b**k-1)


This calls for an explanation. First, let’s assume $b^k-1>1$ since if $b^k-1=1$, both sides evaluate to zero under mod $1$. Expanding the right hand side, we get

We are left with a sum of $n$ powers of $b^k$. Under mod $b^k-1$, each term becomes $1$ so the terms clearly sum to $n$.

Of course, this is a comically inefficient way to compute log and only works for perfect powers. As the title suggests, it has little use outside of code golf. It is very short though. As an example, the shortest expression for $\text{ctz}(x)$ in Python 2 is len(bin(x&-x))-3. But if we limit the domain of the function to 62-bit integers, we can shorten it to (x&-x)**6/63%63.