You will write a prime test, a factorisation into a dictionary of exponents, and a test for perfect powers that reads the exponents.
The idea. A composite number has a prime factor at most its square root. Divide out each factor as many times as it goes, and count. A number is a perfect th power exactly when every exponent is a multiple of .
Your tasks.
is_prime(n). Return True for a prime and False otherwise. Try the divisors d with d * d <= n. Remember that is not prime.factorise(n). Return a dictionary such as {2: 3, 3: 2, 5: 1} for .is_perfect_power(n, k). Return True when every exponent of factorise(n) is a multiple of k.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…