I am trying to compute Nth Squarefree numbers using Mathematica. What I am trying to utilize is the SquareFreeQ[i] function.
Here is my solution :
NSqFree[N_] :=
For[n = 1; i = 1, n <= N + 1,
If[SquareFreeQ[i], If[n == N, Print[i] ] n++] i++]
But I am supposed to compute NSqFree[1000000000] but seems like my approach is taking for ever. Any faster method ?
Thanks,
ADDED:
Here an exactly identical topcoder question and the corresponding editorial for the same.
You have to us the Inclusion-Exclusion principle: suppose you want to find the number of square free numbers up to $N$, then from $N$ you have to substract the number of integers divisible by the square of a prime, but then you have to add any multiple of the square of the product of two discinct primes and so on, in formulas the number looked for is $$ N - \sum_{p^2 \le N} \left\lfloor\frac{N}{p^2}\right\rfloor + \sum_{p^2q^2 \le N} \left\lfloor\frac{N}{p^2q^2}\right\rfloor - \sum_{p^2q^2r^2 \le N} \left\lfloor\frac{N}{p^2q^2r^2}\right\rfloor + \cdots -\cdots $$ using the moebius $\mu$ function you can write this last formula as $$ \sum_{n \le \sqrt{N}} \mu(n) \left\lfloor\frac{N}{n^2}\right\rfloor $$ I don't know how to write this in mathematica but it should take a negligible fraction of the time it takes your current method.