You will convert numbers to and from any base and test numbers for being squares, the slow way and the fast way.
The idea. The remainders of repeated division by are the digits, units first. Going back, multiply by and add the next digit. A square must leave or on division by , and or on division by , so these tests throw out most numbers quickly.
Your tasks.
to_base(n, b). Return the list of digits of n in base b, the first digit first. For n == 0 return [0].from_base(digits, b). Return the number whose digits in base b are digits.could_be_square(n). Return False when the remainder of n on division by 4 or by 9 rules out a square, and True otherwise.Run your code once to unlock the worked solution.
Have you tried it?
Do the tasks in the editor first, and run the checker after each one. Its hints help when a check fails. When you are ready, compare your code with this solution.
Log in to join the discussion.
Loading…