puzzle contents
Contents
raw puzzle

Determine the smallest prime number that is on an even position in a given vector. If such a number does not exist, an appropriate message will be displayed.
Input: an array X[N]
Output: the prime number or string NULL
Example: For X = {9,31,38,5,62,44,38,17,19,38,50,74}, the output is 5.

Solution

Testing if a number is prime can be done with a simple loop or by using the ruby `prime` library. It then is just looping over the numbers array and check if the position is even, the number is prime and if the current number is a new candidate for the minimum.

require 'prime'

def smallest_prime_even_position(numbers)
  min = "NULL"
  numbers.each_with_index {|val, index|
    min = val if index.odd? and Prime.prime?(val) and (min == "NULL" or val < min)
  }
  return min
end

puts smallest_prime_even_position(File.read(ARGV[0]).split(',').map(&:to_i))