working solution for uglynumbers