add solution for fourth problem with some explanation, extended to different numeral systems