1

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, ..., 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 for n, and then just checking my array of bool values

int start = (n % 10 < 7 ? n - (n % 10 + 3) : n - (n % 10 - 7) )
    for (int i = start; i >= 0; i-= 10){ 
        if(primes[i]) 
            return i;
}

It would keep the whole thing simple i guess, but I was wondering what would be the more efficient approach be. Unless there is a way to easily avoid having an array, but I couldn't think of any.

toxing
  • 47
  • 7
  • So you have calculated all the prime numbers using Sieve of Eratosthenes in the primes array ? Can you post the code for it, I have an approach in mind that will completely remove the use of the above code, no need to run this for loop as well. – zenwraight Jun 22 '17 at 22:51
  • I don't have it coded, but yeah. Using sieve I want to look for all primes in range and keep them in primes array as boolean values – toxing Jun 22 '17 at 22:57
  • Got it let me post an implementation of it then here, and how I am planning to work on the case of prime numbers ending in 7 – zenwraight Jun 22 '17 at 22:58
  • I have posted my answer, try it and let me know, if you find something wrong in it – zenwraight Jun 22 '17 at 23:17

1 Answers1

2

Below here, you will find my implementation of Sieve of Eratosthenes algorithm for finding prime numbers between 1 and 250000 and also how I make use of it, to filter out all the prime number ending in 7.

The overall time complexity of this algorithm is O(N) because all the implementation is done in sieve algo.

    import java.io.*;
    import java.util.*;

    public class Main {
        public static void main(String[] args) {
            int N = 250000;

            ArrayList<Integer> primeWithEnding7 = new ArrayList<Integer>();
            int maxPrimeNum7 = 0;
            boolean[] isPrime = new boolean[N + 1];
            for (int i = 2; i <= N; i++) {
                isPrime[i] = false;
            }

            for (int i = 2; i <= N; i++) {
                if (!isPrime[i]) {
                    int rem = i%10;
                    if(rem == 7) {
                      maxPrimeNum7 = Math.max(maxPrimeNum7, i);
                      primeWithEnding7.add(i);
                    }
                    for (int j = i+i; j <= N; j+=i) {
                        isPrime[j] = true;
                    }
                }
            }

            // Print all the prime numbers ending in 7 
            for(int i: primeWithEnding7) {
              System.out.print(i + " ");
            }
            System.out.println();
            System.out.println("Max number is " + maxPrimeNum7);
      }
    }

Now let's take an example to understand why this algorithm will work for us.

So let's suppose N = 30. Now when the loop starts from 2, if 7 was not prime it would have been covered as non-prime in the inner loop j, the fact that i reaches to 7 proves that it's a prime number, So I keep a global array list as my data structure to add only those prime numbers that end in 7 and because I use % operator to calculate the last digit of the number, the time complexity of that step is O(1), so total time complexity of the algorithm comes to O(N).

Let me know, if I have made any mistake in the algorithm, I will fix it.

Hope this helps!

zenwraight
  • 2,002
  • 1
  • 10
  • 14
  • Alright, thanks a lot. I think that could be it. Simple, quick to code yet not messy. I would just swap true and false values with each other, because I think that in your code isPrime[7] actually returns false, but that doesnt really matter here – toxing Jun 22 '17 at 23:22
  • Also quick question. If I'm only looking for the greatest value in primeWithEnding7, maybe I could avoid creating a list and do `int result;` and `if (rem == 7) result = i;`? – toxing Jun 22 '17 at 23:26
  • ohh ya the naming could have been better ya toggle the true and false and let me update, if you want max value ending with 7 – zenwraight Jun 22 '17 at 23:30
  • @zenwraight This implementation of Sieve doesn't have `O(n)` complexity. Its actually `O(n * log n)`. – Sanket Makani Jun 23 '17 at 05:28
  • @SanketMakani O(n*log(log(n)) is more accurate. – Paul Hankin Jun 23 '17 at 08:10