Implement fundamental Algorithm in Ruby