Return the list of the number and its power with python

Before we start our new python project here is another solution for one of the python’s question on codewars. In this example, we need to create a method which will accept a number and returns a list consists of a pair of a number and power that are equal to that input number. For example, the input number is 9, then the number and it’s power which is equals to that input number is [3, 2] because 3 * 3 = 9. If there is no number and power that match the input value then None will be returned. Here is the solution to the above mentioned question.

def isPP(power):
    
    for num in range(1, power):
        for po in range(2, power):
            if(num ** po == power):
                return [num, po]
      
    return None

The above method is only suitable to solve a number input which is below 1800, the program will become freeze if the number goes beyond that value (depends on the processing power of your own computer). Well, hope you like this quick solution, in the next chapter we will begin our project.

2
Leave a Reply

avatar
2 Comment threads
0 Thread replies
2 Followers
 
Most reacted comment
Hottest comment thread
2 Comment authors
thijsGary Capell Recent comment authors
  Subscribe  
newest oldest most voted
Notify of
Gary Capell
Guest
Gary Capell

Instead, find the prime factors of the number.
Stop when you find some prime factors that show that we can’t make a**b -> n.
https://gist.github.com/gcapell/4309fdb0ee281e06bb71a9b694d81711
Finds the answer for 1800 **14 in fraction of a second.

thijs
Guest
thijs

no need to return None, it does that by default.