微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何检查一个数字是否可以表示为 x 的 y 次幂?

如何解决如何检查一个数字是否可以表示为 x 的 y 次幂?

我需要 x^y == 整数(输入) 不知道怎么做 我无法使用数学导入 试图做这样的事情:

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

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

例如 输入是 8 我需要程序来找到 x^y==8 在这种情况下,输出需要是: x=2 y=3 希望它清楚 提前致谢。

解决方法

您可以找到素数因子并取素数计数的最大公分母 (gcd)。

例如 216000 的质因数是 2^6,3^3,5^3 所以幂将是 3。对于每个质数,保持 count/3 作为计算基数的幂:2^2 * 3^ 1 * 5^1 = 60。所以 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

输出:

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)
,

这是一个解决方案:

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])

输出是:

solution is 81 = 3 ^ 4
,

我想我了解您想要实现的目标,这应该可行,但我相信其他人可以为您提供更好的解决方案。

    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
,

要检查数字 a 是否可以是 x 的完美幂,您可以检查

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

然后要找出它是否是任何东西的完美数,从 2 循环到该值的平方根

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,

示例

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

2 (True,2,1)
4 (True,2)
8 (True,3)
9 (True,3,2)
16 (True,4)
25 (True,5,2)
27 (True,3)
,
var SelectedColor = function SelectedColor(props) {
  var style = {
    backgroundColor: props.color
  };
  return /*#__PURE__*/react__WEBPACK_IMPORTED_MODULE_0___default.a.createElement("div",{
    className: "color-selected",style: style
  });
};

var Color = function Color(props) {
  var style = {
    backgroundColor: props.color
  };
  return /*#__PURE__*/react__WEBPACK_IMPORTED_MODULE_0___default.a.createElement("div",{
    className: "color",style: style,onClick: props.handleClick
  });
};

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?