示例#1
0
  def find(digit_set):
    s = set()
    for p in permut(digit_set):
      for c in couple(ops, length=3, optimized=False):
        l = list(p)

        # add ops
        for i,cc in enumerate(c):
          l.insert(2*i+1, cc)

        # for the first parentheses
        for i in range(int(len(l)/2)):
          for j in range(i+1, int(len(l)/2)+2):

            # for the second parentheses
            for ii in range(i):
              for jj in range(i+1, int(len(l)/2)+2):    

                # make a copy
                ll = l[:] 

                # add first parentheses
                ll.insert(2*i, '(')
                ll.insert(2*j, ')')

                # add second parentheses
                # NB: can be optimized but i don't know how... :/
                ll.insert(2*ii, '(')
                ll.insert(2*jj, ')') 

                #raw_input(ll)

                try:
                  v = eval(' '.join(ll))
                  if v > 0 and int(v) == v:
                    s.add(int(v))
                  #print ' '.join(ll), eval(' '.join(ll))
                except:
                  pass
    return s
示例#2
0
def brute_force():
  is_cube = lambda x: x == math.pow(round(math.pow(x,1./3)),3)
  n = 5

  cube_roots = []
  for i in xrange(342,10000):
    if i in cube_roots: 
      print "already checked", i 
      continue
    l = []
    cube = i**3
    print "\n%d" % i
    for p in permut(list(str(cube))):
      p = int(p)
      if p < 41063625: continue
      if is_cube(p) and p not in l:
        l.append(p)
        cube_roots.append(round(p**(1./3)))
        print p,
      if len(l) == n:
        return min(l)

  return "[result]"
示例#3
0
# math considerations
#
# 1+2+3+4+5+6+7+8+9 = 45 which is divisible by 3 so n <> 9
# 1+2+3+4+5+6+7+8 = 36 so n <> 8
# 1+2+3+4+5+6 = 21 so n <> 6
# 1+2+3+4+5 = 15 so n <> 5
# 1+2+3 = 6 so n <> 3
# 1+2 = 3 so n <> 2

import time
from permut import *
from prime import *


def is_pandigital(x):
    return "".join(sorted(str(x))) == "123456789"


t = time.time()

m = 0
for n in [7, 4]:
    for p in permut(range(n, 0, -1)):
        if is_prime(int(p)):
            m = max(m, p)
    if m:
        break

print m
print "time: %f s" % (time.time() - t)
示例#4
0
#     primes.append(int(p))
#     if len(primes) == 12:
#       print "eleven found! break for p =", p
#       break

def fit(p):
  p = str(p)
  return all(is_prime(int(p[:-i])) and is_prime(int(p[i:])) for i in range(1,len(p)))

# test
#print len([c for c in couple(range(1,5), 4)])

primes,r = set(), [2]+range(1,10,2)
for n in range(2,10):
  for c in couple(r, n):
    for p in permut(c):
      if p[0] in ['1','9'] or p[-1] in ['1','9']: continue
      if is_prime(int(p)) and fit(int(p)):
        primes.add(int(p))
        print p, c, len(primes)
      if len(primes) == 11:
        break
    if len(primes) == 11:
      break
  if len(primes) == 11:
    print "11 found !!!"
    break

print "sum:", sum(primes)
print primes
print "time: %f s" % (time.time() - t)
示例#5
0
#
# There are no arithmetic sequences made up of three 1-, 2-, or 3-digit primes, 
# exhibiting this property, but there is one other 4-digit increasing sequence.
#
# What 12-digit number do you form by concatenating the three terms in this sequence?

import time
from collections import defaultdict

from prime import *
from permut import *

t = time.time()

for c in couple(range(10), length=4):
  primes = set([int(p) for p in permut(c) if len(str(int(p))) == 4 and is_prime(int(p))])
  if len(primes) <= 2:
    continue
  
  l = sorted(list(primes))
  dist = list( ((p1,p2),p2-p1) for p1 in l for p2 in l if p2>p1 )
  dist.sort(key=lambda x:x[0][0])
  seqs = defaultdict(list)
  for p1p2,d in dist:
    if not seqs[d] or any(p1p2[0]==pp1pp2[1] for pp1pp2 in seqs[d]):
      seqs[d].append(p1p2)
  for k,v in seqs.items():
    if len(v) <= 1:
      del seqs[k]
    v.sort(key=lambda x: x[0])
  if seqs:
示例#6
0
# Let d1 be the 1st digit, d2 be the 2nd digit, and so on. In this way, we note the following:
#
# d2d3d4=406 is divisible by 2
# d3d4d5=063 is divisible by 3
# d4d5d6=635 is divisible by 5
# d5d6d7=357 is divisible by 7
# d6d7d8=572 is divisible by 11
# d7d8d9=728 is divisible by 13
# d8d9d10=289 is divisible by 17
# Find the sum of all 0 to 9 pandigital numbers with this property.

from permut import *
import time


def is_pandigital(x):
    return "".join(sorted(str(x))) == "0123456789"


t = time.time()

s, r = 0, [2, 3, 5, 7, 11, 13, 17]
for p in permut(range(0, 10)):
    if len(int(p)) != 10:
        continue
    if all(int(p[i + 1 : i + 1 + 3]) % d == 0 for i, d in enumerate(r)):
        s += int(p)

print s
print "time: %f s" % (time.time() - t)