This sort of thing is what is (un-)affectionately known as “Perl golf.” The programmer used grep because s/he thought it was “more efficient,” and then used or instead of an if-statement because, when using grep, he had to.
And, if that programmer were a “lone wolf,” not part of a team, this sort of coding might be quasi-okay. Twenty-five years ago, it was necessary. Today, we have microprocessors that do hundreds of millions of ops per second, but programmers are as expensive as they always were. Please, today, just be clear.
If anyone would not immediately understand the use of grep and hashes to calculate prime numbers (and, who would?), please don’t code it that way (anymore).