Message Boards Message Boards

0
|
3388 Views
|
4 Replies
|
2 Total Likes
View groups...
Share
Share this post:

How to mark primes in a List?

Posted 10 years ago

How to mark special items in a List? e.g. Range[5]^2 + 1 = {2, 5, 10, 17, 26} I want to output {Framed[2], Framed[5], 10, Framed[17], 26} , how to do?

More , how to mark prime vertexs in a Graph plot?

POSTED BY: Cody Luo
4 Replies

A similar resolution is to use SetDelay (:>) : Range[10]^2 + 1 /. p_ /; PrimeQ[p] :> Framed[p]. SetDelay gives you more controls. ex. what if the condition is that $p-3$ is prime then I add frame to p ? You can do:

Range[10]^2 + 1 /. p_ /; PrimeQ[p-3] :> Framed[p]

POSTED BY: Shenghui Yang
Posted 10 years ago

If[PrimeQ[#], Style[#, Red], #] & /@ (Range[25]^2 + 1)

NOT be: If[PrimeQ[#], Style[#, Red], #] & /@ Range[25]^2 + 1

POSTED BY: Cody Luo
 Range[5]^2 + 1 /. p_?PrimeQ -> Framed[p]
POSTED BY: Ilian Gachevski

Exercise for the interested student: use the following functions to solve this problem...

PrimeQ

Map

If

;-)

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