1
$\begingroup$

The code I have thus far to my question is here:

primes[m_] :=
(Clear[list, primes];
list = {};
For[k = 1,
k^2 < 1000, 
k = k + 1, 
If[PrimeQ[k^2 < 1000], AppendTo[list, k]]];
list);
primes[30]

I changed my code but it is still wrong. I will get this after some trial and error, hopefully.

$\endgroup$
  • 4
    $\begingroup$ Select[Range[Sqrt[1000]], PrimeQ] $\endgroup$ – march Oct 13 '16 at 17:49
  • $\begingroup$ Look at the syntax for a For loop: For[start, test, incr, body]. You need four inputs. You only have two specified. $\endgroup$ – march Oct 13 '16 at 17:50
  • 1
    $\begingroup$ I love the Select command but this is a specific task I must do using loops (Even though Select command is essentially running a loop in the background), thanks for the advice, I will mess around with it! $\endgroup$ – Brandon Oct 13 '16 at 17:52
  • 1
    $\begingroup$ see Loop alternatives $\endgroup$ – BlacKow Oct 13 '16 at 17:52
  • 1
    $\begingroup$ Okay, I can help you with that. But also, for fun: Prime /@ Range@PrimePi[Sqrt[1000]]. $\endgroup$ – march Oct 13 '16 at 17:53
3
$\begingroup$

Direct fix for your For loop here, I would say:

Module[{list = {}, k},
  For[k = 1, k < Sqrt[1000], k++, If[PrimeQ[k], AppendTo[list, k]]];
  list
 ]

Using Sow and Reap rather than appending to a list (which is slow):

Last@Last@Reap@Module[{list = {}, k},
  For[k = 1, k < Sqrt[1000], k++, If[PrimeQ[k], Sow[k]]];
  list
 ]

Do is better than For:

Last@Last@Reap@Module[{list = {}, k},
Do[If[PrimeQ[k], Sow[k]], {k, Sqrt[1000]}];
list
]

The loop-like construction I would use to do this problem:

Last@Last@Reap@Module[{n = 1}, 
  While[n < Sqrt[1000], If[PrimeQ[n++], Sow[n - 1]]]
 ]

Alternatively,

Select[Range[Sqrt[1000]], PrimeQ]

and, my favorite and probably the fastest,

Prime /@ Range@PrimePi[Sqrt[1000]]
| improve this answer | |
$\endgroup$
  • $\begingroup$ Great! although I am very unfamiliar with module, sow and reap. I appreciate your method! I'm still trying to go about it in my original way. I have edited my original code above but still doesn't work. $\endgroup$ – Brandon Oct 13 '16 at 18:35
  • $\begingroup$ Module is a scoping construct. It basically does the thing you're trying to do with the Clear command by making local versions of the variables that don't leak out into the global variable space (i.e once the function is completed, list is left undefined). Sow and Reap are super-great. You should learn about them. $\endgroup$ – march Oct 13 '16 at 20:25
  • $\begingroup$ Clear[list]; list = {}; For[ k = 1, k^2 < 1000, k = k + 1, If[PrimeQ[k], AppendTo[list, k]];]; list $\endgroup$ – Brandon Oct 13 '16 at 20:25
  • $\begingroup$ @Brandon. Yep, that does it. (Also, see my previous comment: I forgot to ping you on it.) $\endgroup$ – march Oct 13 '16 at 20:26
  • $\begingroup$ I was able to figure out my original code, its right above this comment. Thanks again for sticking through this with me! $\endgroup$ – Brandon Oct 13 '16 at 20:26
0
$\begingroup$

Just another way:

Prime[Range@PrimePi[Ceiling@Sqrt[1000]]]
| improve this answer | |
$\endgroup$

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.