Formula for the number of numbers $\le n$ with same prime factors as $n$?

Let $\omega(n)$ (OEIS A001221) represent the number of distinct prime factors of $n$. Then a (fairly) transparent expression for what you want is

$$\sum_{1 \leqslant k \leqslant n} [\omega(k) = \omega(n) = \omega(\gcd(k,n))],$$

where $[\cdot]$ is the Iverson bracket, equal to $1$ when the condition inside is satisfied and $0$ otherwise. So we're tallying up the $k$s which have precisely the same number of prime factors as $n$ and no extra ones. Note that $\operatorname{lcm}$ would work just as well: $\omega(\gcd(k,n))$ is the number of distinct prime factors shared by both $k$ and $n$; $\omega(\operatorname{lcm}(k,n))$ is the number of distinct primes present in either $k$ or $n$ or both.

It really is necessary to specify the second equality, otherwise you end up including terms like $15=5 \cdot 3$ for $n=24 = 2^3 \cdot 3$. Notice that $\omega(\operatorname{lcm}(15,24)) = 3 \ne 2$ and $\omega(\gcd(15,24)) = 1 \ne 2$.

Another (perhaps more clear) way is to simply write

$$\sum_{1 \leqslant k \leqslant n} [p | n \Leftrightarrow p | k, \text{ for all prime }p].$$


(I prefer the notation $\displaystyle \sum_{j=1}^n 1_{j | n^j} 1_{ n | j^n} $, and using that for integers $\lfloor \frac{a}{b}\rfloor-\lfloor \frac{a-1}{b}\rfloor= 1_{b|a}$, and $rad(n) = \prod_{p | n} p$)

we get $$1_{ a | b^a} = 1_{ rad(a) | b} = 1_{ rad(a) | rad(b)}$$ thus

$$1_{j | n^j} 1_{ n | j^n} = 1_{ rad(j) | rad(n)}1_{ rad(n) | rad(j)} = 1_{ rad(j)= rad(n)}$$ and $rad(j)= rad(n),\ j \le n$ iff $j = d \ rad(n)$ with $rad(d) | rad(n)$ i.e. iff $d | n^d$ hence $$\displaystyle \sum_{j=1}^n 1_{ j | n^j} 1_{ n | j^n} = \sum_{j=1}^n 1_{ rad(j) = rad(n)} = \sum_{d=1}^{n/rad(n)} 1_{rad(d)|rad(n)} = \sum_{d=1}^{n/rad(n)} 1_{d | n^d}$$

and I'm not sure there is much simpler anymore, since this $\sum_{d=1}^{n/rad(n)} 1_{rad(d)|rad(n)}$ seems to depend on the precise prime factors of $n$, say if $p | n$, it depends on the maximum power of $p$ that is $\le \frac{n}{rad(n)}$, and hence on the other prime factors of $n$