Message Boards Message Boards

1
|
4708 Views
|
11 Replies
|
12 Total Likes
View groups...
Share
Share this post:

Using a 'Do' loop to redefine my 'For' loop function?

Posted 8 years ago

I am trying to produce this same output using a Do loop. Here is the code I have for my For loop.

primelist[m_] := 
 (Clear[ourlist]; 
  ourlist = {}; 
  For[
   k = 1, (* Initial k value,  this is where we start*)
   k <= m, (* For loop will stop at the last prime number less than or equal to whatever we specify for "m", this is what were testing *)
   k = k + 1,(* value we are testing to determine if prime or not prime, this is the increments *)
   If[PrimeQ[k], AppendTo[ourlist, k]] (* appends k to our empty list defined as ourlist = {}, this is where the prime numbers will be stored in list form, this is the body *)
   ];
  ourlist)
primelist[25]

here is the output as expected:

{2, 3, 5, 7, 11, 13, 17, 19, 23}

I know For loops are most common, but I also know you can use a For, Do and While loop to basically do the same thing if you wanted. I'm trying to find a way using the Do loop in my case.

Thanks for any help,

B

POSTED BY: Brandon Davis
11 Replies
Posted 8 years ago

Very informative Thanks to the both of you,

B

POSTED BY: Brandon Davis

But if you insist on using a Do loop here is an example:

primes = {};
Do[
  If[PrimeQ[i], AppendTo[primes, i]],
  {i, 25}];
primes
POSTED BY: David Reiss
Posted 8 years ago

Using a 'Do' loop to redefine my 'For' loop function.

POSTED BY: Brandon Davis
Posted 8 years ago

Hey David,

I use 'For' loops frequently in R, but I am trying to get a feel for loops in different programs, for this example, in Mathematica. I'm at a beginners level in Mathematica but the logic is very similar to other programs. I didn't even know it could be done the first way. I appreciate both methods. I just know how useful and widely used loops are that I am trying to practice through various methods, such as For, While and Do loops. Thanks again!

Is using "While" similar to the "Do" code you created? I tried now using While but it seems a bit harder. Well, I know all 3 loops have different syntax, etc.

Brandon

POSTED BY: Brandon Davis

The solution by David is very good. But an even more efficient way is actually directly asking for primes: so no Select or For or Do necessary:

Prime[Range[PrimePi[25]]]

PrimePi[25] will give show that the lowest prima equal or below 25 is the 9th prime. Then the numbers 1 through 9 are created {1,2,3,4,5,6,7,8,9}, and then for each, a certain prime is asked.

POSTED BY: Sander Huisman

True! But to implement your last sentence would involve a Select or a Cases line of code. So, it depends on what one wants the function to return.

POSTED BY: David Reiss

No it does not, look at the code. No need for Cases, DeleteCases, Select, If or so... It doesn't eliminate elements, Basically PrimePi will figure out the number of primes, and then those are asked.

POSTED BY: Sander Huisman

Oh, right, my bad ;-)

POSTED BY: David Reiss

The best thing to do for leaning Mathematica is not to try to implement the coding paradigms of other languages. Although they exist in Mathematica for the sake of completeness (and, yes, sometimes they are the right choice for something very particular), the paradigms in Mathematica are in Funcitonal, pattern matching, and rule-based prorramming--and the language for these (I may have left something out), or at least how they are combined into one language is unique to Mathematica. The best thing for a beginner to do to learn the language is to use one of the excellent books out there or to use Stephen Wolfram's recent book which is on-line at

http://www.wolfram.com/language/elementary-introduction/

Start there and go through it step by step to get the sense of how to write code in Mathematica

POSTED BY: David Reiss

Agreed, 'translating' languages to Mathematica almost always leaves you with slow, verbose, low-level code... There are exceptions of course...

POSTED BY: Sander Huisman

Is there a reason why you are using procedural programming to do this? The Wolfram Language is much more powerful than this and typically things like For and Do loops are rarely needed. For example

Select[ Range[25], PrimeQ]
POSTED BY: David Reiss
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract