2

I'm currently doing a unit at university that touched briefly on Mathematica, but the final exam will feature multiple choice questions from Mathematica (along with excel and matlab). This leads to this question

Range[10] /. {x_ /; PrimeQ[x] -> x^2}

What does this actually do? I plugged it into Mathematica, and it gave me

{1, 4, 9, 4, 25, 6, 49, 8, 9, 10}

as its output.

I lack the understanding of what each part of the input does as I never came across this earlier in my studies.

m_goldberg
  • 107,779
  • 16
  • 103
  • 257
  • By x2, you really meant to say x^2, yes? Then all you're doing is replace any number that is prime with its square. – J. M.'s missing motivation Jun 08 '15 at 10:00
  • Based on the output shown in OP I've edited the OP to have x^2. – LLlAMnYP Jun 08 '15 at 10:20
  • 1
    This could also be written Range[10] /. {x_?PrimeQ -> x^2} – Bob Hanlon Jun 08 '15 at 11:26
  • I really don't see any reason to close this. It's not due to a 'simple mistake'; there is no mistake. Nor is an explanation of the OP expression 'easily found in the documentation'; it can found there, but not easily. There are two good, up-voted answers that are useful to future visitors. This question should be reopened. – m_goldberg Jun 08 '15 at 13:09
  • @m_goldberg The answers are very useful for the OP (I upvoted both), but otherwise it is a localized question. What to do with any upcoming code snippets a user wants reverse-engineered or explained in detail? I would not agree that searching is difficult here. – Yves Klett Jun 08 '15 at 15:46
  • @m_goldberg While the entire structure is not "easily found in the documentation" it is nevertheless a rather basic operation that is covered in myriad other Q&A's on this site. We have typically not been highly receptive to "explain this code for me" posts unless there was something unusual exhibited, if I am not mistaken. How about marking this as a duplicate of (25616)? – Mr.Wizard Jun 09 '15 at 00:10
  • @YvesKlett Please see my comment directly above. – Mr.Wizard Jun 09 '15 at 00:10

2 Answers2

6

Range[10] - Make a list of integers from 1 to 10.

/. - Replace all occurrences of

{x_ - anything, to which we will refer to as x

/; - so long as the test

PrimeQ[x] - for primality yields True

-> - with

x^2} - the square of itself.

LLlAMnYP
  • 11,486
  • 26
  • 65
6

There's a lot of syntactic sugar in there. I'd like to add an answer to show you how you can make progress yourself when encountering unknown syntax.

If you wrap the code in FullForm[Hold[...]] Mathematica will show you the functions all these operators stand for:

FullForm[Hold[Range[10] /. {x_ /; PrimeQ[x] -> x^2}]]
(* Hold[ReplaceAll[Range[10], List[Rule[Condition[Pattern[x, Blank[]], PrimeQ[x]], Power[x, 2]]]]] *)

If you place your cursor in any of the unknown functions and hit F1, the docs will show you the details about these functions and also which operators are available for these. By matching the full form with the short form, we can figure some things out:

From this, we hopefully piece things together: the code create a range from 1 to 10 and then replaces every prime with its square.

Martin Ender
  • 8,774
  • 1
  • 34
  • 60