We study the asymptotics of the number N(t) of geometrically distinct closed geodesics of a Riemannian or Finsler metric on a connected sum of two compact manifolds of dimension at least three with non-trivial fundamental groups and apply this result to the prime decomposition of a three-manifold. In particular we show that the function N(t) grows at least like the prime numbers on a compact 3-manifold with infinite fundamental group. It follows that a generic Riemannian metric on a compact 3-manifold has infinitely many geometrically distinct closed geodesics. We also consider the case of a connected sum of a compact manifold with positive first Betti number and a simply-connected manifold which is not homeomorphic to a sphere.