Added a shorter naive version that is O(n^2).