A positive integer n is a perfect power if it can be expressed as the power be for some two integers b and e that are both greater than one. (Any positive integer n can always be expressed as the trivial power n1, so we don’t care about those.) For example, the integers 32, 125 and 441 are perfect powers since they equal 25, 53 and 212, respectively.
This function should determine whether the positive integer n is a perfect power. Your function needs to somehow iterate through a sufficient number of possible combinations of b and e that could work, returning True right away when you find some b and e that satisfy be == n, and returning False when all relevant possibilities for b and e have been tried and found wanting.
Since n can get pretty large, your function should not examine too many combinations above and beyond those that are both necessary and sufficient to reliably determine the answer. Achieving this efficiency is the central educational point of this problem.
Input: Integer (int).
Output: Logic value (bool).
assert perfect_power(8) == True assert perfect_power(42) == False assert perfect_power(441) == True assert perfect_power(469097433) == True
- n > 0.
Related to the mission, you may be interested at Catalan’s conjecture, these days a proven mathematical theorem that says that after the special case of the two consecutive perfect powers 8 and 9, whenever a positive integer n is a perfect power, n – 1 is never a perfect power. For example, we don’t have to slog through all potential ways to express the number as an integer power to know from the get-go that 1234567890-1 is not a perfect power. This also illustrates the common asymmetry between performing a computation to opposite directions. Given some big chungus integer such as 4922235242952026704037113243122008064, but not the formula that originally produced it, it is not quite easy to tell whether that integer is a perfect power, or some perfect power plus minus one.
The mission was taken from Python CCPS 109. It is taught for Ryerson Chang School of Continuing Education by Ilkka Kokkarinen
CheckiO Extensions allow you to use local files to solve missions. More info in a blog post.
In order to install CheckiO client you'll need installed Python (version at least 3.8)
Install CheckiO Client first:
pip3 install checkio_client
Configure your tool
checkio --domain=py config --key=
Sync solutions into your local folder
(in beta testing) Launch local server so your browser can use it and sync solution between local file end extension on the fly. (doesn't work for safari)
checkio serv -d
Alternatevly, you can install Chrome extension or FF addon
checkio install-plugin --ff
checkio install-plugin --chromium