Solved GrayCode problem with math