Beispiel #1
0
def sols_for_one_pow(n):
 """
 Counts all solutions to the diophantine equation 1/a+1/b=p/10^n
 All variables are positive integers, n is fixed
 """
 tot = 0
 pow5 = [5**i for i in xrange(n+2)]
 pow2 = [2**i for i in xrange(n+2)] 
 for e1 in xrange(n+1):
  for e2 in xrange(n+1):
   a,b = pow5[e1],pow2[e2]
   mul = pow5[n-e1]*pow2[n-e2]
   tot += count_divs(shanks_factorize(mul*(a*b+1)))
   if e1 and e2:
    tot += count_divs(shanks_factorize(mul*(a+b)))
 return tot
Beispiel #2
0
def m_search(n,k,m=1,phi=1,lowerInd=0):
 global primes,mask,pcap
 s = 0
 if k == 0:
   M = m*phi-phi+m
   bnd = int(M**.25)
   pi = bisect_right(primes,n/m)
   if n/m > pcap:
    f = shanks_factorize(M,primes[:15])
    for d in divisors([[bas,f[bas]] for bas in f]):
     q,r = divmod(d-phi,m-phi)
     if r == 0 and q > primes[lowerInd] and q*m <= n and is_prime(q,pcap-1,mask):
       if (q*m-1) % (q*m - (q-1)*phi) == 0:
        s += q*m
   else:
    for q in primes[lowerInd+1:pi+1]:
     if q*m <= n and (q*m-1) % (q*m - (q-1)*phi) == 0:
      s+= q*m
#        print q*m,q,m
   return s
 bound = int((n/m)**(1./(k+1)))
 i=lowerInd+1
 for i,p in enumerate(primes[lowerInd+1:],lowerInd+1):
   if p > bound:
    break
   else:
    s += m_search(n,k-1,m*p,phi*(p-1),i)
  
 return s   
Beispiel #3
0
def cheat(n):
  nums = np.loadtxt("e245.txt",dtype=np.int64)
  nums = nums[:,1]
  for i in xrange(nums.shape[0]):
   f = shanks_factorize(nums[i])
   if len(f) >= 4:
     print nums[i],f
  return sum(nums[np.where(nums<=n)])
Beispiel #4
0
def solve_1(cap,mod=10**9+7):
 primes = primes_and_mask(cap)[0]
 R = [1 for i in xrange(cap+1)]
 N = [i**4+4 for i in xrange(cap+1)]

 sq = [0,0,0,0]
 for p in primes:
  if p == 2:
    for i in xrange(1,cap+1):
      mul = 1
      while N[i]%2==0:
       N[i]/=2
       mul*=2
      if mul>1:
       R[i] = (R[i]*(mul+1))%mod
    continue
  sq[0] = mod_sqrt(p-4,p)
  if not sq[0]:
   continue
  sq[2] = mod_sqrt(sq[0],p)
  sq[3] = mod_sqrt(p-sq[0],p)
  sq[0] = (p-sq[3])%p
  sq[1] = (p-sq[2])%p
 # print p,sq
  for q in xrange(0,cap+1,p):
    for r in sq:
      if r and r <= cap-q:
        mul=1
        while N[q+r]%p==0:
         mul *= p
         N[q+r]/=p
        if mul > 1:
         R[q+r] = (R[q+r]*(mul+1))%mod
 print "Completed seiving."
 c=0
 s = 0
 for i,r in enumerate(R):
  if i==0:
   continue
  n = N[i]
  if n ==1:
    s =  (s + r -i**4-4)%mod
    continue
  factors = shanks_factorize(n)
  for f in factors:
    if f>1:
     r = (r*(f**factors[f]+1))%mod
  if len(factors)>=2:
   c +=1
  s =  (s + r -i**4-4)%mod
 print s 
 print c
 return s
Beispiel #5
0
def find_pairs(n):
  global primes,mask
  s = 0
  pairs = {}
  for p in primes[1:]:
   num = p**2-p+1
   f = shanks_factorize(num,primes[:15])
   for k in divisors([[bas,f[bas]] for bas in f] ):
    q = num/k - p + 1
    if q > 0 and q > p and q*p <= n and is_prime(q,pcap-1,mask):
      s += p*q
#      print p,q
      if p not in pairs:
       pairs[p] = [q]
      else:
       pairs[p].append(q)
  print "Sum over semi-primes %d" % s  
  #now do backtracking pair-combos
  for p1 in pairs:
   for p2 in pairs[p1]:
    s += cores_backtrack(n/p1/p2,pairs,[p1,p2])
  return s
Beispiel #6
0
def r_slow(n):
 r=1
 factors = shanks_factorize(n)
 for f in factors:
   r *= f**factors[f]+1
 return r-n 
Beispiel #7
0
                #k choose i,  then i choose j+i-k
                target[k-1] += C(k,i) * C(i, j+i-k) * a[i-1]*b[j-1]
    return target

for i in xrange(200):
    p_rows.append(pascal_row(i+2))

bound = 10**16
classes, primes = prime_equivalences(bound)
tot = 0
for c in classes:
    g_rep = p_rows[c[0]-1]
    for el in c[1:]:
        if not el:
            break
        else:
            g_rep = combine_gozinta(g_rep, p_rows[el-1])
    g = sum(g_rep)
    if g > bound:
        continue
#    print g, c
    fact = shanks_factorize(g)
    rep = fact.values()
    trunc_rep = sorted(rep)[::-1]
    for i in xrange(len(rep), len(c)):
        trunc_rep.append(0)
    if trunc_rep == c:
        print g, fact, trunc_rep, c
        tot += g
print "Total "+str(tot)
Beispiel #8
0
def f(n):
  f = shanks_factorize(n)
  return max(f.keys())