How to find reverse of pow(a,b,c) in python?

前端 未结 2 1323
别跟我提以往
别跟我提以往 2021-01-25 19:27

pow(a,b,c) operator in python returns (a**b)%c . If I have values of b, c, and the result of this operation (res=pow(a,

2条回答
  •  时光说笑
    2021-01-25 20:26

    The best you can do is something like this:

    a = 12
    b = 5
    c = 125
    
    def is_int(a):
        return a - int(a) <= 1e-5
    
    # ============= Without C ========== #
    print("Process without c")
    rslt = pow(a, b)
    
    print("a**b:", rslt)
    
    print("a:", pow(rslt, (1.0 / b)))
    
    # ============= With C ========== #
    print("\nProcess with c")
    rslt = pow(a, b, c)
    
    i = 0
    while True:
    
        a = pow(rslt + i*c, (1.0 / b))
    
        if is_int(a):
            break
        else:
            i += 1
    
    print("a**b % c:", rslt)
    print("a:", a)
    

    You can never be sure that you have found the correct modulo value, it is the first value that is compatible with your settings. The algorithm is based on the fact that a, b and c are integers. If they are not you have no solution a likely combination that was the original one.

    Outputs:

    Process without c
    a**b: 248832
    a: 12.000000000000002
    
    Process with c
    a**b % c: 82
    a: 12.000000000000002
    

提交回复
热议问题