solved problem 1 with C, trying problem 3 with Ruby