Make a list of the first 100 primes, keeping only ones whose last digit is less than 3 [closed]

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!

• Select[Prime[Range[100]], Mod[#, 10] < 3 &]
– ydd
Commented Jun 6, 2023 at 16:16
• 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. Commented Jun 6, 2023 at 16:17
• heh, or Mod--that's simpler. But now you don't get to try it on your own. Commented Jun 6, 2023 at 16:17
• sorry to ruin the fun :( should've just suggested Mod instead of explicitly typing it all out
– ydd
Commented Jun 6, 2023 at 16:19
• If you want a list of 100 such primes: primes = GeneralUtilitiesNewIterator[MyPrimeIterator, {n = 0, p = 0}, While[Mod[p = Prime[++n], 10] >= 3, Null]; p ]; GeneralUtilitiesTakeIterator[primes, 100] // ReadList Commented Jun 8, 2023 at 0:12

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]

• Without Composition your answer gets a bit more readable: Select[Prime[Range[100]], Last[IntegerDigits[#]] < 3 &] Commented Jun 6, 2023 at 16:50
• sure, but I said explicitly that I was providing a point-free solution. Commented Jun 6, 2023 at 17:03
• 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. Commented Jun 6, 2023 at 17:08
• I am aware that my comment concerning the readability is subjective. Commented Jun 7, 2023 at 12:24
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}

• love this one!! Commented Jun 6, 2023 at 17:05

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]&]]


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

p = Prime[Range[100]]


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


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

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}

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]&)


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