A bit late but this c# code will do it. It finds the most uniform (least ragged) solution for a given number of lines, or a max line length.
It works by using recursion to bifurcate at each word separator. Then it sorts the potential solutions by lowest-standard-deviation-of-line-length, and returns the first solution.