3
$\begingroup$

This is a question from an "Elementary Introduction to the Wolfram Language" Section 28: Tests and Conditionals. We are asked to "Make a list of the first 100 primes, keeping only ones whose last
digit is less than 3."

I think it should look something like this:

Select[Prime[Range[100]], f[#]<3 &]

where f[#] specifies a function that would select the last digit of each number.

However, I do not know how to create a condition on the last digit of a number?

Would somebody be able to help with this? Or point out where I've gone wrong so far. Thanks!

$\endgroup$
5
  • 3
    $\begingroup$ Select[Prime[Range[100]], Mod[#, 10] < 3 &] $\endgroup$
    – ydd
    Jun 6 at 16:16
  • 2
    $\begingroup$ IntegerDigits will give you a list of the digits that represent an integer. The Last function will give you the last element of a list. And LessThan will do a comparison for you. Try to put those together into a function. Come back with more questions if you still can't figure it out. $\endgroup$
    – lericr
    Jun 6 at 16:17
  • 2
    $\begingroup$ heh, or Mod--that's simpler. But now you don't get to try it on your own. $\endgroup$
    – lericr
    Jun 6 at 16:17
  • 1
    $\begingroup$ sorry to ruin the fun :( should've just suggested Mod instead of explicitly typing it all out $\endgroup$
    – ydd
    Jun 6 at 16:19
  • $\begingroup$ If you want a list of 100 such primes: primes = GeneralUtilities`NewIterator[MyPrimeIterator, {n = 0, p = 0}, While[Mod[p = Prime[++n], 10] >= 3, Null]; p ]; GeneralUtilities`TakeIterator[primes, 100] // ReadList $\endgroup$
    – Michael E2
    Jun 8 at 0:12

6 Answers 6

8
$\begingroup$

Well, since the cat's out of the bag, I guess we can proceed with our race-toward-ten. I'll provide a point-free solution to the original problem and avoid the OP's question about the specific f:

Select[Prime[Range[100]], LessThan[3]@*Last@*IntegerDigits]
$\endgroup$
4
  • 1
    $\begingroup$ Without Composition your answer gets a bit more readable: Select[Prime[Range[100]], Last[IntegerDigits[#]] < 3 &] $\endgroup$ Jun 6 at 16:50
  • 1
    $\begingroup$ sure, but I said explicitly that I was providing a point-free solution. $\endgroup$
    – lericr
    Jun 6 at 17:03
  • 1
    $\begingroup$ Also, I always bristle when people assert that something's more readable as if there's an objective standard. I personally find the Composition expression more readable then the Function one. $\endgroup$
    – lericr
    Jun 6 at 17:08
  • $\begingroup$ I am aware that my comment concerning the readability is subjective. $\endgroup$ Jun 7 at 12:24
9
$\begingroup$
SolveValues[x <= Prime[100] && Mod[x, 10] < 3, x, Primes]
 {2, 11, 31, 41, 61, 71, 101, 131, 151, 181, 191, 211, 241, 251,   
  271, 281, 311, 331, 401, 421, 431, 461, 491, 521, 541}
$\endgroup$
1
  • 2
    $\begingroup$ love this one!! $\endgroup$
    – lericr
    Jun 6 at 17:05
6
$\begingroup$

Expanding on my comment (first method)

1.

Select[Prime[Range[100]], Mod[#, 10] < 3 &]

here are two more (kind of cheating because 2 is an exception)

2.

Join[{2}, Select[Prime[Range[100]], Mod[#, 10] ==1 &]]
Join[{2},1+Select[Prime[Range[100]]-1,Divisible[#,10]&]]
$\endgroup$
5
$\begingroup$

Another option using what the question asks, which is to use f[#]

p = Prime[Range[100]]

Mathematica graphics

f[n_Integer?Positive] := Last[IntegerDigits[n]]
Select[p, f[#] < 3 &]

Mathematica graphics

9 more different ways to do this are possible in Mathematica.

$\endgroup$
4
$\begingroup$

Just to illustrate Sow and Reap

Flatten[Reap[
   Sow[#, Mod[#, 10]] & /@ Prime[Range[100]], _?(#1 <= 3 &)][[2]]]

yields:

{2, 3, 13, 23, 43, 53, 73, 83, 103, 113, 163, 173, 193, 223, 233,
263, 283, 293, 313, 353, 373, 383, 433, 443, 463, 503, 523, 11, 31,
41, 61, 71, 101, 131, 151, 181, 191, 211, 241, 251, 271, 281, 311,
331, 401, 421, 431, 461, 491, 521, 541}

$\endgroup$
2
$\begingroup$

As all valid primes other than 2 must end in 1:

{2}~Join~(Prime[Range[100]]//Pick[#,Mod[#,10],1]&)

(* {2, 11, 31, 41, 61, 71, 101, 131, 151, 181, 191, 211,
 241, 251, 271, 281, 311, 331, 401, 421, 431, 461, 491, 521, 541} *)

Alternatively:

{2}~Join~(Prime[Range[100]]//Pick[#,NumberDigit[#,0],1]&)

Original Answer

Prime[Range[100]]//Pick[#,Clip[Mod[#,10]-3],-1]&
$\endgroup$

Not the answer you're looking for? Browse other questions tagged or ask your own question.