C solution for 39