尤拉計劃717:取模公式之和

lt發表於2020-05-24

enter image description here

測試程式:

import time
P=[3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97]
def f(p):
 return (2**(2**p)//p)%(2**p)

def g(p):
 return (2**(2**p)//p)%(2**p)%p

def G(N):
 s=0
 for p in P:
  if p>=N:break
  s+=g(p) 
 return s

t=time.time();print(G(10));print(time.time()-t)

轉載別人的

import time
def prime(n):
 primes=[]
 h  = [True] * int(n+1)
 h[:2] = [False, False]
 for i in range(2, int(n ** (1.0*0.5)) + 1):
    if h[i]:
        h[i*i::i] = [False] * len(h[i*i::i])
 for i, e in enumerate(h):
    if e:
        primes.append(i)
 return primes
#P=[3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97]


def g(p):
 a = power_mod(2, p, p*(p-1))
 b = (a - p) % (p*(p-1))
 c = power_mod(2, a, p*p) // p
 d = power_mod(2, b, p*p) // p
 return (c - 2*d) % p

def power_mod(a, n, k ):
    d = 1;
    a = a%k
    while n > 0:
     if(n & 1)!=0:
       d = (d*a)%k
     a = (a*a)%k
     n >>= 1
    return d

def G(N):
 s=0
 P=prime(N)
 for p in P:
  if p>=N:break
  #print(p,g(p))
  s+=g(p) 
 return s

t=time.time();print(G(10));print(time.time()-t)

相關文章