-7

I need x^y == integer (input) cant figure out how to do so I CANT USE MATH IMPORT tried to do something like this:

a = int(input("Please Enter any Positive Integer:"))
 power = 1
 i = 1

 while(i <= a):
     power = power * a
    i = i + 1

for example the input is 8 i need the program to find x^y==8 in this case the output needs to be: x=2 y=3 hope its clear thanks in advance.

pioneer
  • 1
  • 2

5 Answers5

1

You can find prime factors and take the greatest common denominator (gcd) of prime counts.

For example 216000's prime factors are 2^6, 3^3, 5^3 so the power will be 3. For each of the primes keep count/3 as the power to compute the base: 2^2 * 3^1 * 5^1 = 60. So 216000 = 60^3

def primeFactors(N):   # returns dictionary of {prime:count}
    result = dict()
    p = 2              # p is a candidate prime factor   
    while p*p<=N:      # prime candidates up to √N (remaining N) 
        while N%p == 0:   # count prime factors
            result[p] = result.get(p,0)+1 
            N //= p       # by removing factors, only primes will match
        p += 1 + (p&1)    # next potential prime
    if N>1: result[N] = 1 # anything remaining after √N is a prime
    return result

def gcd(a,b=0,*c):                    # gcd using Euclid's algorithm
    if c: return gcd(gcd(a,b),*c)     # for multiple values
    return a if not b else gcd(b,a%b) # Euclidian division version

def findPower(N):
    counts = primeFactors(N)       # {prime factor: count}
    power  = gcd(*counts.values()) # power is gcd of prime counts
    base   = 1                  
    for f,c in counts.items():     # compute base
        base *= f**(c//power)      # from remaining prime powers
    return base,power

output:

print(findPower(216000))           # (60,3)
print(findPower(8))                # (2,3)
print(findPower(81))               # (3,4)
print(findPower(371293))           # (13,5)
print(findPower(29**7))            # (29,7)
print(findPower(1522756**5553))    # (1234, 11106) 
print(findPower(12345**12345))     # (12345,12345)
Alain T.
  • 40,517
  • 4
  • 31
  • 51
0

Here is a solution:

def findfactors(number):
    factors = []
    search = number
    for n in range(2, number//2):
        while search%n==0:
            factors.append(n)
            search = search // n
    if search != 1:
        factors.append(search)
    return factors


def findxpowery(number):
    res = findfactors(number)
    diffactors = set(res)
    
    #print("factors: ", res)
    #print("different factors", diffactors)
    
    res2 = [res.count(x) for x in diffactors]
    #print("count the different factors", res2)
    
    
    rr = 1
    for r in diffactors:
        rr = rr*r
    #print("multiplicate all the different factors", rr)
    
    
    
    if len(set(res2))==1:
        #print("solution is", rr, "^", list(set(res2))[0])
        return (rr, list(set(res2))[0])
    else:
        #print("no solution")
        return None

number = (9**2)
r = findxpowery(number)
print("solution is", number, "=", r[0], "^", r[1])

Output is :

solution is 81 = 3 ^ 4
Malo
  • 1,233
  • 1
  • 8
  • 25
0

I think I understand what you are trying to achieve, this should work, I'm sure someone else can give you a better solution though.

    a = int(input("Please Enter any Positive Integer:"))
    max_ = round(a**(1/2)+1)


    for x in range(2, max_):
        for y in range(2, max_):
    
            p = x**y
            if p == a:
        
                print(x,'power',y)
                print(p)
        
            elif p > a:
                pass
razor3001
  • 39
  • 4
-1

To check if a number a can be a perfect power of x you may check

x ** int(round(math.log(a, x))) == a

Then to find out if there it is a perfect number of anything, loop from 2, to the square root of the value

from math import sqrt, log, ceil

def find_perfect_power(a):
    for value in range(2, ceil(sqrt(a)) + 1):
        if value ** int(round(log(a, value))) == a:
            return True, value, int(round(math.log(a, value)))
    return False,

Example

for i in range(1, 30):
    r = find_perfect_power(i)
    if r[0]:
        print(i, r)

2 (True, 2, 1)
4 (True, 2, 2)
8 (True, 2, 3)
9 (True, 3, 2)
16 (True, 2, 4)
25 (True, 5, 2)
27 (True, 3, 3)
azro
  • 53,056
  • 7
  • 34
  • 70
-1
num = int(input("enter number: ")) 
# if you have x and y values for example x = 5 and y = 5
x = 5 
y = 5 
if x ** y == num:
    print("True") 
else:
    print("False")