Solution using Ruby's Prime class.