Let's suppose $n$ is an integer around 250000. Using Java, I need to find the greatest prime number that ends with 7 and belongs to ${1, \dots, n}$. Also, I need to keep an eye on computational complexity and try to lower it as much as I can. So I was thinking of using Sieve of Eratosthenes and looping through an array of boolean values and returning first one from the end that $\mod 10$ returns $7$.
It would keep the whole thing simple I guess, but I was wondering what would the more efficient approach be. Unless, there is a way to easily avoid having an array, but I couldn't think of any. Any ideas for an efficient algorithm?
About $1$ in $\log n$ numbers is prime around $n$. For $n=250000$ that is about $1$ in $13$, so it is much more efficient to just start downward from $n$ and try the primes. You can do simple division tests to eliminate multiples of $3, 7$ and a few other small primes, then use the Fermat primality test. You can look up on the internet the few examples where the Fermat test fails and hardcode them, or you can use one of the tests that guarantees primality. You won't have to look far. This is much more efficient than sieving.
I tried explicitly for $n=250000$ and only had to try four. $249967$ is prime.