Extract the index which has the next greater prime
int x = - 1 ; for ( int j = 0 ; j < primes . Count ; j ++ ) { if ( arr [ i ] == primes [ j ] ) { x = j ; break ; } else if ( arr [ i ] < primes [ j ] ) { x = j ; break ; } }