The separation problem for p-groups (resp. nilpotent groups) reduces to the computation of the closure of a rational language in the profinite topology associated with p-groups (resp. nilpotent groups). In the p-group case, this reduces to computing the closure of a finitely generated subgroup. The algorithm in this case uses automata-theoretic methods, as well as (a small amount of) linear algebra, and it can be performed in polynomial time. The nilpotent case follows from the p-group case (since every finite nilpotent group is a direct product of p-groups) but we lose the polynomial time bound. Extending this result to the next natural class, namely solvable groups, seems to be very difficult.