我的任务是创建一个函数来总结元组的幂。
def sumOfPowers(tups, primes):
x = 0;
for i in range (1, len(primes) + 1):
x += pow(tups, i);
return x;
到目前为止,我有这个。
tups - 一个或多个元组的列表,素数 - 一个或多个素数的列表
它不起作用,因为输入是元组而不是单个整数。我怎么能解决这个问题才能让它适用于列表呢?
[/编辑] 样本输出:
sumOfPowers([(2,3), (5,6)], [3,5,7,11,13,17,19,23,29]) == 2**3 + 5**6
True
sumOfPowers([(2,10**1000000 + 1), (-2,10**1000000 + 1), (3,3)], primes)
27
[(2,4),(3,5),( - 6,3)]的幂和为2 ^ 4 + 3 ^ 5 +( - 6)^ 3
**素数的目的是在输入的列表中的每个素数上执行^ k1 + ... a ^ kn模的计算。 (也就是执行每个输入所指定的和计算,以模拟第二个输入列表中的每个素数,然后使用中国余数定理求解)
示例输入中使用的Primes列表:
15481619,15481633,15481657,15481663,15481727,15481733,15481769,15481787
,15481793,15481801,15481819,15481859,15481871,15481897,15481901,15481933
,15481981,15481993,15481997,15482011,15482023,15482029,15482119,15482123
,15482149,15482153,15482161,15482167,15482177,15482219,15482231,15482263
,15482309,15482323,15482329,15482333,15482347,15482371,15482377,15482387
,15482419,15482431,15482437,15482447,15482449,15482459,15482477,15482479
,15482531,15482567,15482569,15482573,15482581,15482627,15482633,15482639
,15482669,15482681,15482683,15482711,15482729,15482743,15482771,15482773
,15482783,15482807,15482809,15482827,15482851,15482861,15482893,15482911
,15482917,15482923,15482941,15482947,15482977,15482993,15483023,15483029
,15483067,15483077,15483079,15483089,15483101,15483103,15483121,15483151
,15483161,15483211,15483253,15483317,15483331,15483337,15483343,15483359
,15483383,15483409,15483449,15483491,15483493,15483511,15483521,15483553
,15483557,15483571,15483581,15483619,15483631,15483641,15483653,15483659
,15483683,15483697,15483701,15483703,15483707,15483731,15483737,15483749
,15483799,15483817,15483829,15483833,15483857,15483869,15483907,15483971
,15483977,15483983,15483989,15483997,15484033,15484039,15484061,15484087
,15484099,15484123,15484141,15484153,15484187,15484199,15484201,15484211
,15484219,15484223,15484243,15484247,15484279,15484333,15484363,15484387
,15484393,15484409,15484421,15484453,15484457,15484459,15484471,15484489
,15484517,15484519,15484549,15484559,15484591,15484627,15484631,15484643
,15484661,15484697,15484709,15484723,15484769,15484771,15484783,15484817
,15484823,15484873,15484877,15484879,15484901,15484919,15484939,15484951
,15484961,15484999,15485039,15485053,15485059,15485077,15485083,15485143
,15485161,15485179,15485191,15485221,15485243,15485251,15485257,15485273
,15485287,15485291,15485293,15485299,15485311,15485321,15485339,15485341
,15485357,15485363,15485383,15485389,15485401,15485411,15485429,15485441
,15485447,15485471,15485473,15485497,15485537,15485539,15485543,15485549
,15485557,15485567,15485581,15485609,15485611,15485621,15485651,15485653
,15485669,15485677,15485689,15485711,15485737,15485747,15485761,15485773
,15485783,15485801,15485807,15485837,15485843,15485849,15485857,15485863
答案 0 :(得分:1)
我不确定我是否理解你,但也许你正在寻找这样的事情:
from functools import reduce
def sumOfPowersModuloPrimes (tups, primes):
return [reduce(lambda x, y: (x + y) % p, (pow (b, e, p) for b, e in tups), 0) for p in primes]
您不应该遇到任何内存问题,因为您的(中间)值永远不会超过max(primes)
。如果结果列表太大,则返回生成器并使用它而不是列表。
答案 1 :(得分:0)
def sumOfPowers (powerPairs, unusedPrimesParameter):
sum = 0
for base, exponent in powerPairs:
sum += base ** exponent
return sum
或简短:
def sumOfPowers (powerPairs, unusedPrimesParameter):
return sum(base ** exponent for base, exponent in powerPairs)
执行每个输入所指定的和计算,以模拟第二个输入列表中的每个素数
这是完全不同的事情。但是,您仍然没有真正解释您的功能应该做什么以及它应该如何工作。鉴于你提到了欧拉定理和中国余数定理,我想它还有很多比你实际让我们相信的要多。您可能希望通过使用欧拉定理来减少那些大功率来求解取幂。我不想进一步猜测发生了什么事;这似乎涉及到你应该首先在论文中解决的一个非平凡的数学问题。
def sumOfPowers (powerPairs, primes):
for prime in primes:
sum = 0
for base, exponent in powerPairs:
sum += pow(base, exponent, prime)
# do something with the sum here
# Chinese remainder theorem?
return something
答案 2 :(得分:0)
忽略primes
,因为它们似乎没有用于任何事情:
def sumOfPowers(tups, primes):
return sum( pow(x,y) for x,y in tups)
您是否有可能计算一个或多个素数的模和?像
这样的东西2**3 + 5**2 mod 3 = 8 + 25 mod 3 = 33 mod 3 = 0
(其中a+b mod c
表示在除以a+b
)后取余数余下的c
。
关于如何使用多个素数的一个猜测是使用素数的乘积作为 除数。
def sumOfPower(tups, primes):
# There are better ways to compute this product. Loop
# is for explanatory purposes only.
c = 1
for p in primes:
p *= c
return sum( pow(x,y,c) for x,y in tups)
(如果a mod pq == (a mod p) mod q
和p
都是素数,我似乎还记得q
,但我可能会弄错。)
另一种方法是为每个素数返回一个总和:
def sumOfPower(tups, primes):
return [ sum( pow(x,y,c) for x,y in tups ) for c in primes ]