Various algorithms in ruby