0

我正在尝试以十六进制格式获取结果,但我收到错误“TypeError:'float' object cannot be mapped as an integer!”

     39 d = chinese_remainder(a, n)
---> 40 number = hex(d)
     41 print(number)

代码:

import functools

# Euclidean extended algorithm
def egcd(a, b):
    if a == 0:
        return b, 0, 1
    else:
        d, x, y = egcd(b % a, a)
        return d, y - (b // a) * x, x

"""
    Functions whcih calculate the CRT (
    return x in ' x = a mod n'.
"""

def chinese_remainder(a, n):
    modulus = functools.reduce(lambda a, b: a * b, n)
    multipliers = []
    for N_i in n:
        N = modulus / N_i
        gcd, inverse, y = egcd(N, N_i)
        multipliers.append(inverse * N % modulus)

    result = 0
    for multi, a_i in zip(multipliers, a):
        result = (result + multi * a_i) % modulus
    return result

FN = 1184749
FM = 8118474
FL = 5386565
HN = 8686891
HM = 6036033
HK = 6029230

n = [FN, FM, FL]
a = [HN, HM, HK]

d = chinese_remainder(a, n)
number = hex(d)
print(number)

结果应该是这样的 FAB15A7AE056200F9

但它给了我 3.3981196080447865e + 19

如何解决此问题以使结果为十六进制格式???

4

1 回答 1

0

普通除法/运算符返回浮点数,而您可以使用地板除法//来获取整数。N正如其他人所建议的那样,您必须像这样使用地板除法来变量N = modulus//N_i

于 2021-10-19T00:59:25.007 回答