optimized prob 3 to O(n^0.5)