0

I have three versions to with Module and a simpler one without to compute the sum of primes. However it will only return a value of 1. Not sure what to do? I thought Return[j,Module] would do the trick. see Returning a value from a Module

using

SumPrime[8]

to call them.

SumPrime[n_] :=  Module[{x = n}, 
  For[{i = 1, j = 1}, {i <= PrimePi[n], Return[j, Module]}, {Set[j, j + Prime[i]], i++}] 
]

SumPrime[n_] :=  Module[{x = n}, 
  For[{i = 1, j = 1}, {i <= PrimePi[n], Return[j, Module]}, {j = j + Prime[i], i++}] 
]

SumPrime[n_] :=  For[{i = 1, j = 1}, {i <= PrimePi[n], Return[j]}, {j = j + Prime[i], i++}]
m_goldberg
  • 107,779
  • 16
  • 103
  • 257
onepound
  • 343
  • 1
  • 9
  • check the syntax for For – user42582 Dec 21 '17 at 15:14
  • 4
    Mathematica is not python. You don't need an explicit Return. And For is not your friend (just search on this site about For to learn why). Your code can be written in a much cleaner way with Do, if it can't be written with functional programming. If you want a chunk of code that does exactly what you want here, but is faster and more readable, this will do it (assuming I got the gist of what you wanted to do): sumPrime[n_] := Total[Table[Prime[i], {i, PrimePi[n]}]] – b3m2a1 Dec 21 '17 at 15:16
  • @ b3m2a1 thanks, I need to develop it further so I also want to subtract off each prime number and take the log so I need to manipulate each prime...Looking up Table[]. – onepound Dec 21 '17 at 15:27
  • @b3m2a1 yes seems that will work thanks again. – onepound Dec 21 '17 at 15:32
  • mathematica is not c either. Curly brackets are list delimiters (only). I can't make sense of why you are using them here. Also disagree with first comment, dont even waster your time learning For syntax. – george2079 Dec 21 '17 at 15:51
  • to group things. like {i = 1, j = 1}. – onepound Dec 21 '17 at 15:53
  • 2
    if you just want to evaluate things as a group use semicolons and no parenthesis. i=1;j=1 This is called a Compound Expression http://reference.wolfram.com/language/ref/CompoundExpression.html. – george2079 Dec 21 '17 at 15:59

2 Answers2

4

Rather than iterative looping you can take advantage of the Listable attribute of many built-in functions:

sumPrime[n_] :=Total@Prime[Range[PrimePi[n]]]
Mike Honeychurch
  • 37,541
  • 3
  • 85
  • 158
  • I think you need PrimePi in there: Total@Prime@Range@PrimePi@n – george2079 Dec 22 '17 at 17:26
  • @george2079 correct. thanks. rushed it. was wanting to show listability rather than looping – Mike Honeychurch Dec 22 '17 at 22:43
  • 1
    Good on showing the listability. Might be good to make a quick note of it though in the body text. The OP seems like they know very little about how Mathematica works as a language. – b3m2a1 Dec 22 '17 at 22:59
3

You For-loop is ill-formed; it does not follow the syntax rules for Mathematica's For.

This will work.

sumPrime[n_] :=
  Module[{i, j},
    For[i = 1; j = 0, i <= PrimePi[n], i++, j += Prime[i]];
    j]

10

Note that there is no need to use Return because Module automatically returns the result of evaluation its body (its 2nd argument), which is j. However, I recommend

sumPrime[n_] := Total @ Table[Prime[i], {i, PrimePi[n]}]

which is both simpler and quicker.

sumPrime[5]

10

m_goldberg
  • 107,779
  • 16
  • 103
  • 257