I am looking for an algorithm to quickly calculate the sum of the cubes of primes. More formally, this function: $$ f(n) = \sum_{p \in P} p^3, P = \{ p \in [2, n]: p \text{ is prime} \}$$
In my case, $n$ is somewhere around $10^{12}$, so the naive approach of sieving for primes, then adding their cubes is too slow and memory prohibitive. What would be some more efficient approaches that could still give me the exact solution?