4
$\begingroup$

These four commands

ParallelSum[Prime[i]^2, {i, 1, 10000} ] // AbsoluteTiming
ParallelSum[Prime[i] + Prime[i + 1], {i, 1, 10000} ] // AbsoluteTiming
ParallelSum[ Prime[i] + Prime[i + 1] + Prime[i + 2], {i, 1,  10000} ] // 
AbsoluteTiming
ParallelSum[Prime[i] + Prime[i + 1] + Prime[i + 2] + Prime[i + 3], {i, 1, 
10000} ] // AbsoluteTiming

gives out nearly the same time.

but when I change 10000 to 100000

ParallelSum[Prime[i]^2, {i, 1, 100000} ] // AbsoluteTiming
ParallelSum[Prime[i] + Prime[i + 1], {i, 1, 100000} ] // AbsoluteTiming
ParallelSum[
Prime[i] + Prime[i + 1] + Prime[i + 2], {i, 1,  100000} ] // AbsoluteTiming
ParallelSum[Prime[i] + Prime[i + 1] + Prime[i + 2] + Prime[i + 3], {i, 1, 
100000} ] // AbsoluteTiming

enter image description here

what makes the difference?

$\endgroup$
  • 1
    $\begingroup$ I guess that coupling ParalellSum and Prime is not especially constructive in order to understand the underlying issues. Prime is based on PrimePi which uses so called Lagaris Miller Odlyzko method finding primes. This is a sophisticated algorithm using sparse caching and sieving. You should perform extensive analysis for much larger numbers. There is yet another issue related to Prime which has quite different timings. You should carefully test this post What is so special about Prime? $\endgroup$ – Artes Feb 6 at 12:07
  • 1
    $\begingroup$ Many parameters are automatically chosen in Parallel.... The argument might have a great influence on the parameters. I would suggest forcing as many parameters as possible to ensure a fair comparison. See e.g. mathematica.stackexchange.com/questions/55242/… $\endgroup$ – anderstood Feb 6 at 13:53
1
$\begingroup$

Are you interested in rapidly summing primes? or was that just an example to illustrate a ParallelSum question?

If you want fast methods for summing primes please see this question and its answers.

Please note that the following is a much faster way to sum the sequence of 4 adjacent primes, the fourth example in your question.

With[{m = 10000}, AbsoluteTiming[
   4*ParallelSum[Prime[i], {i, 1, m + 3}] -
              3*(Prime[1] + Prime[m + 3]) -
              2*(Prime[2] + Prime[m + 2]) -
                (Prime[3] + Prime[m + 1])
   ]]
| improve this answer | |
$\endgroup$
  • $\begingroup$ An example to illustrate a ParallelSum question. btw, thx for your code, quite fast. $\endgroup$ – wuyudi Feb 7 at 4:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.