Prime trivia
April 24, 2025 at 1:12 PM by Dr. Drang
I was at a trivia contest last night, and one of the questions was: What is the largest three-digit prime number? One of my teammates and I both guessed 997 and went about trying to prove or disprove it before the next question came up.
We agreed that 997 wasn’t a multiple of 7. My thinking was that since
and
and 17 isn’t a multiple of 7, 997 isn’t a multiple of 7. Similarly, since
997 isn’t a multiple of 11, either. I was trying to work out my reasoning for 13, starting with
when we decided to just go with 997 as our answer because time was running out. Later I realized that I should have used a different multiple of 13 and done the subtraction in the other direction:
It’s more obvious—to me, anyway—that 43 isn’t a multiple of 13 than that 217 isn’t.
Despite our failure to check if it was a multiple of 13 (or any higher prime), we got the answer right.
If you’re feeling an itch to tell me some rules about checking divisibility, don’t bother. As Matt Parker said in this video, there are an endless number of them, and I just don’t see myself committing any of them—other than the rule for 3, which I’ve known since I was a kid—to memory. Integer arithmetic doesn’t show up much in structural or mechanical engineering and has never seemed natural
This morning, I decided to look into how Mathematica handles primes. One function, Prime[n]
, returns the nth prime number, and another, PrimePi[x]
gives the number of primes less than or equal to x. It gets its name from the prime counting function,
I put these functions together like this,
Prime[PrimePi[999]]
to get 997, which is a reasonably convenient way to get the largest prime less than or equal to a number. But what if I wanted to get the five largest three-digit primes?
I could work my way down the ladder.
Prime[PrimePi[996]]
returns 991, and
Prime[PrimePi[990]]
returns 983. But this is tedious, and there should be a way to get them all at once. One way is to use the Table[]
function to get a list of all the three-digit primes,
Table[Prime[n], {n, PrimePi[999]}]
and then pull out just the last five:
Table[Prime[n], {n, PrimePi[999]}][[-5;;]]
This returns a list comprising 971, 977, 983, 991, and 997. I find Mathematica’s list indexing notation hard to remember, mainly because everything is doubled. The brackets have to be doubled because Mathematica uses single brackets to enclose function arguments. And the double semicolons are a single term; the expression
[[-5;;]]
means “start 5 items from the end and go to the end.” It’s like
[-5:]
in Python, only harder to read.
Although it’s not obvious from the documentation, Prime[]
can take a list of integers as its argument and will return the corresponding list of primes. So
Prime[{1, 2, 3, 4, 5, 6}]
returns the list comprising 2, 3, 5, 7, 11, and 13. We can use this and the Range[n]
function to simplify our expression for the five largest three-digit primes:
Prime[Range[PrimePi[999]]][[-5;;]]
OK, it’s not that much simpler. I often think Mathematica and Perl are too heavily influenced by TMTOWTDI.
-
Yes, that’s an intentional pun. ↩