Project Euler, bash version

OK, so when I’m bored I sometimes attack a few of the problems on Project Euler; so far I’ve done about 40-odd because I’m slow and inept. I just finished problem 41, which is to find the highest pandigital1 number that is prime. I did my program in Haskell, of course, but in the thread someone had this:

primes 1234567 7654321 | grep 1 | grep 2 | grep 3 \
  | grep 4 | grep 5 | grep 6 | grep 7 | tail -n 1

I don’t think I need to tell you that this is the best answer ever. And it’s far faster than mine…


  1. The Wikipedia definition seems to allow multiple occurrences of the same digit; obviously PE didn’t (otherwise there would be infinitely many and thus no ‘largest’). Also, you were allowed numbers less than 123456789 as long as an n-digit number had all the digits 1–n, for example 4231. Which is good since the answer had seven digits. 

Tags: ,

Leave a Reply

You must be logged in to post a comment.