0

我的任务是创建一个函数来对元组的幂求和。

    def sumOfPowers(tups, primes):
        x = 0;
        for i in range (1, len(primes) + 1):
            x += pow(tups, i);
        return x;

到目前为止,我有这个。

tups - 一个或多个元组的列表,素数 - 一个或多个素数的列表

它不起作用,因为输入是元组而不是单个整数。我该如何解决这个问题以使其适用于列表?

[/edit] 示例输出:

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

**素数的目的是对输入的列表中的每个素数进行 a^k1 + ... a^kn 的计算。(又名执行每个输入指定的和计算模第二个输入列表中的每个素数,然后使用中国剩余定理求解)

示例输入中使用的素数列表:

     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
4

3 回答 3

2

我不太确定我是否正确理解你,但也许你正在寻找这样的东西:

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). 如果您的结果列表太大,则返回一个生成器并使用它而不是列表。

于 2013-10-14T22:26:10.883 回答
1

忽略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+bc


关于如何使用多个素数的一种猜测是使用素数的乘积作为除数。

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 qifpq都是素数,但我可能弄错了。)


另一种方法是为每个素数返回一个总和:

def sumOfPower(tups, primes):
    return [ sum( pow(x,y,c) for x,y in tups ) for c in primes ]
于 2013-10-14T22:15:40.250 回答
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
于 2013-10-14T21:43:42.337 回答