This method has the limitation that it will work for only those matrix where the first integer of each row is greater than the last integer of the previous row.
The sieve of Eratosthenes is an ancient algorithm for finding all prime numbers up to any given limit. It does so by iteratively marking numbers in the range as composite by marking the multiples of each prime as non-prime.