dynamic programming, large cases passed
use n < x/n instead of n*n < x to avoid overflow