import math
from sympy.ntheory import factorint
def prime_test(num):
if num < 2:
return False
elif num == 2:
return True
elif num != 2 and num % 2 == 0:
return False
else:
for i in range(3, int(num ** (1/2)) + 1, 2):
if num % i == 0:
return False
return True
def fermat_theorem(A, B, M):
new_B = int(B % (M - 1))
return (A, new_B, M)
def eulers_theorem(A, B, M):
totient = M
factors_of_M = [(i-1) / i for i in factorint(M).keys()]
for i in factors_of_M:
totient *= i
new_B = int(B % totient)
return (A, new_B, M)
for line in open("tex.txt", "r"):
w = (line.split(" "))
A, B, M = int(w[0]), int(w[1]), int(w[2].strip())
if prime_test(M): # Fermats Theorem
for i in range(10):
A, B, M = fermat_theorem(A, B, M)
print(A, B, M)
else: # Using eulers totient function
for i in range(10):
A, B, M = eulers_theorem(A, B, M)
print(A, B, M)
例如编号(A,B,M)是
600092825 1030270503 271050379
679689384 1055455905 210287267
我们需要找到(A ** B) % M
我不知道为什么,但是递归中有问题。仅经过1次处理后,这些功能始终会给我相同的答案。为什么会这样呢?
谢谢
编辑:由于数字太大,我期望B减少。 例如,
((123 ** 562) % 100)
缩小为((123 ** 2) % 100)
。 ((123,562,100)至(123,2,100))
但是在我的( 600092825, 1030270503, 271050379)
代码中
600092825 217218375 271050379
600092825 217218375 271050379
600092825 217218375 271050379
600092825 217218375 271050379
....
因此,在第一步B减小,但即使递归也减小了停止。我的问题是为什么B停止下降?