solution for 047 in JS. should really do it again in C