Polya猜想的一个反例

时间:2014-07-05 12:51:47

标签: python mathematical-optimization discrete-mathematics

Polya's conjecture是一个数学猜想,假设第一个(-1)^(Omega(n))的总和,其中Omega(n)是具有多重性的n的素数的除数,总是负的或零。

一个反例是906316571,是五十年前发现的。我想知道他们怎么能找到它,因为它需要大量的时间,我试图优化我的python算法,但它仍然需要很长的时间,你能帮我优化吗?

这是我的代码(我使用了memoization)

 >>> class Memoize:
def __init__(self, f):
    self.f = f
    self.memo = {}
def __call__(self, *args):
    if not args in self.memo:
        self.memo[args] = self.f(*args)
    return self.memo[args]

 >>> def sieve(m):
n=m+1;
s=[];
for i in range(2,n):
    s.append(i);
k=0;
while k<len(s):
    for i in range(2,int(n/s[k])+1):
        x=i*s[k];
        if s.count(x)==1:
            s.remove(x);
    k=k+1;
return s;
>>> s=sieve(100000);
>>> def omega(n):
k=0;
if n==1:
    return 0;
else :
    while k<len(s) and n%s[k]!=0 :
        k=k+1;
    if k<len(s):
        return omega(int(n/s[k]))+1;
    else :
        return 1;
>>> omega=Memoize(omega)
>>> def polya(n):
h=omega(n);
if n==1:
    return 0;
else :
    if omega(n)%2==0:
        return polya(n-1)+1;
    else :
        return polya(n-1)-1;
>>> polya=Memoize(polya);
>>> while polya(k)<=0 :
k=k+1;

1 个答案:

答案 0 :(得分:1)

正如chepner告诉你的那样,1958年的原始证据并非蛮力。它也没有揭示破坏规则的最小数量,它只发现于1980年。我根本没有研究过这个案例,但1980年的证据可能是用计算机完成的。它更多的是可用RAM的数量问题,而不是处理速度问题。

但是,对于现代计算机来说,用蛮力来解决这个问题不应该是非常困难的。 Python不是最好的选择,但数字仍然可以在合理的时间内找到。

import numpy as np
import time

max_number = 1000000000

# array for results
arr = np.zeros(max_number, dtype='int8')

# starting time
t0 = time.time()

# some tracking for the time spent
time_spent = []

# go through all possible numbers up to the larges possible factor
for p in range(2, int(np.sqrt(max_number))):
    # if the number of factors for the number > 0, it is not a prime, jump to the next
    if arr[p] > 0:
        continue
    # if we have a prime, we will have to go through all its powers < max_number
    n = p
    while n < max_number:
         # increment the counter at n, 2n, 3n, ...
        arr[n::n] += 1
        # take the next power
        n = n * p
    # track the time spent

print "Time spent calculating the table of number of factors: {0} s".format(time.time()-t0)

# now we have the big primes left, but their powers are not needed any more
# they need to be taken into account up to max_number / 2
j = 0
for p in range(p + 1, (max_number + 1) / 2):
    if arr[p] > 0:
        continue
    arr[p::p] += 1
    if j % 10000 == 0:
        print "{0} at {1} s".format(p, time.time()-t0)
    j += 1

print "Primes up to {0} done at {1} s".format(p, time.time()-t0)
# now we have only big primes with no multiples left, they will be all 1
arr[arr == 0] = 1

print "Factor table done at {0} s".format(time.time() - t0)

# calculate the odd/even balance, note that 0 is not included and 1 has 0 factors
cumulative = np.cumsum(1 - 2 * (arr[1:] & 1), dtype='int32')
print "Total time {0} s".format(time.time()-t0)

这不是最快或最优化的功能,这背后的数学应该是非常明显的。在我的机器(i7)上运行一个核心需要大约2800秒来计算最多1 x 10 ^ 9的素数因子的完整表格。 (但要注意,如果没有64位python和至少8 GB RAM,请不要尝试这种方法。累积和表消耗4 GB。)

为了证明上述功能至少能很好地起作用,下面是一个有趣区域的图表:

enter image description here

由于第一个数字存在一些问题,上面代码给出的结果略有偏差。要获得官方的Liouville lambda,请使用cumulative[n-1] + 2。对于问题中提到的数字(906 316 571),结果是cumulative[906316570] + 2等于829,这是该地区的最大值。