2014-11-04 62 views
0

这是公式a^b = c(mod m) a = 122 b = 177 m = 197 c =?x = math.fmod(c,m)OverflowError:int太大而无法转换为float

这是Python代码;

a=int(input("Lütfen üssünü almak istediğiniz sayıyı giriniz: ")) 

b=int(input("Lütfen sayının üssü değerini giriniz: ")) 

m=int(input("Lütfen hesaplanacak Mod değerini giriniz: ")) 

import math 

c=a**b 

x=math.fmod(c,m) 

print(x) 

但是我得到一个错误。你可以帮我吗?

x=math.fmod(c,m) 
OverflowError: int too large to convert to float 

回答

1

由于在Python整数运算从不溢出,你可以使用整数模运算符(%),而不是fmod()

print ((122**177) % 197) 

然而,有可能是何人所为您希望您分配这个问题应用模运算的这个性质:

(x * y) % m == ((x % m) * y) % m 

所以,如果我们经常使用模运算符,我们不会rflow,即使在有限整数的编程语言中也是如此:

product = 1 
for i in range(177): 
    product = (product * 122)%197 
print product 
相关问题