예제 #1
1
def gen(l, m, i):
    others = list(range(10))
    others.remove(i)
    past = set()
    ll = [True]*m+[False]*(l-m)
    for u in permutations(ll):
        if u in past: continue
        past.add(u)
        if u[-1] and i % 2 == 0: continue
        for v in product(*([others]*(l-m))):
            if not u[-1] and v[-1] % 2 == 0: continue
            n, k = 0, 0
            for j in u:
                n *= 10
                if j:
                    n += i
                else:
                    n += v[k]
                    k += 1
            yield n
예제 #2
0
def get_ret3(abc):
    ss = set()
    for i in permutations(abc):
        a, b, c = i[0], i[1], i[2]
        for r in get_ret2(a, b):
            ss.add(tuple(sorted((r, c))))
    return set(r for ab in ss for r in get_ret2(*ab))
예제 #3
0
 def test_xy(x, y):
     global max_value
     for f in permutations(range(10), len_active_k):
         for j in range(len_active_k):
             full_f[active_k[j]] = f[j]
         if test(x) > 0 and test(y) > 0:
             max_value = max(max_value, test(x), test(y))
예제 #4
0
 def test_xy(x, y):
     global max_value
     for f in permutations(range(10), len_active_k):
         for j in range(len_active_k):
             full_f[active_k[j]] = f[j]
         if test(x) > 0 and test(y) > 0:
             max_value = max(max_value, test(x), test(y))
예제 #5
0
def gen(l, m, i):
    others = list(range(10))
    others.remove(i)
    past = set()
    ll = [True] * m + [False] * (l - m)
    for u in permutations(ll):
        if u in past: continue
        past.add(u)
        if u[-1] and i % 2 == 0: continue
        for v in product(*([others] * (l - m))):
            if not u[-1] and v[-1] % 2 == 0: continue
            n, k = 0, 0
            for j in u:
                n *= 10
                if j:
                    n += i
                else:
                    n += v[k]
                    k += 1
            yield n
예제 #6
0
#What 12-digit number do you form by concatenating the three terms in this sequence?

#Answer:
#296962999629

from time import time

t = time()
from mathplus import sieve, permutations

ret = {}
primes = sieve(10000)
p = [i for i, f in enumerate(primes) if f and i >= 1000]
for n in p:
    c = []
    for i in permutations(
        (n // 1000, (n % 1000) // 100, (n % 100) // 10, n % 10)):
        if 0 in i: break
        m = i[0] * 1000 + i[1] * 100 + i[2] * 10 + i[3]
        if primes[m] and m not in c: c.append(m)
    if len(c) >= 3:
        c = sorted(c)
        if c[0] in ret: continue
        for i in range(1, len(c) - 1):
            for j in range(i):
                if c[i] * 2 - c[j] in c:
                    ret[c[0]] = (c[j], c[i], 2 * c[i] - c[j])
assert len(ret) == 2
del ret[1487]
print(['%s%s%s' % (v[0], v[1], v[2]) for k, v in ret.items()][0])  #, time()-t
예제 #7
0
#!/usr/bin/python
# -*- coding: utf-8 -*-

#We shall say that an n-digit number is pandigital if it makes use of all the digits 1 to n exactly once. For example, 2143 is a 4-digit pandigital and is also prime.

#What is the largest n-digit pandigital prime that exists?

#Answer:
#7652413

from time import time
t = time()
from mathplus import first_factor, permutations, reduce

p = 0
for ps in permutations(range(7, 0, -1)):
    if ps[0] == 5: continue
    p = reduce(lambda x, y: 10 * x + y, ps)
    if first_factor(p) == p:
        break

print(p)  #, time()-t
예제 #8
0
#Answer:
#16695334890

from time import time

t = time()
from mathplus import permutations, reduce


def three(d, i):
    return d[i] * 100 + d[i + 1] * 10 + d[i + 2]


ret = []
for d in permutations(range(10)):
    if d[5] != 0 and d[5] != 5: continue
    if d[3] % 2 != 0: continue
    #if three(d, 1) % 2 != 0: continue
    if (d[2] + d[3] + d[4]) % 3 != 0: continue
    #if three(d, 2) % 3 != 0: continue
    #if three(d, 3) % 5 != 0: continue
    if (d[4] * 2 + d[5] * 3 + d[6]) % 7 != 0: continue
    #if three(d, 4) % 7 != 0: continue
    if (d[5] - d[6] + d[7]) % 11 != 0: continue
    #if three(d, 5) % 11 != 0: continue
    if (d[6] * 9 - d[7] * 3 + d[8]) % 13 != 0: continue
    #if three(d, 6) % 13 != 0: continue
    if (-2 * d[7] - 7 * d[8] + d[9]) % 17 != 0: continue
    #if three(d, 7) % 17 != 0: continue
    ret.append(reduce(lambda x, y: 10 * x + y, d))
예제 #9
0
    if a != 0: r.append(b/a)
    if b != 0: r.append(a/b)
    return r

@memorize
def get_ret3(abc):
    ss = set()
    for i in permutations(abc):
        a, b, c = i[0], i[1], i[2]
        for r in get_ret2(a, b):
            ss.add(tuple(sorted((r, c))))
    return set(r for ab in ss for r in get_ret2(*ab))

for k in combinations(range(1, 10), 4):
    ss = set()
    for i in permutations(k):
        a, b, c, d = i[0], i[1], i[2], i[3]
        for r in get_ret2(a, b):
            ss.add(tuple(sorted((r, c, d))))
    ret = set(r for abc in ss for r in get_ret3(abc))
    i = 1
    while True:
        if i not in ret: break
        i += 1
    i -= 1
    if i > s:
        s = i
        abcd = sorted(k)


print(''.join(str(i) for i in abcd))#, s, time()-t)
예제 #10
0
def direct_no_thinking(n, size):
    return list(permutations(range(size)))[n]
예제 #11
0
#!/usr/bin/python
# -*- coding: utf-8 -*-

#We shall say that an n-digit number is pandigital if it makes use of all the digits 1 to n exactly once. For example, 2143 is a 4-digit pandigital and is also prime.

#What is the largest n-digit pandigital prime that exists?

#Answer:
	#7652413

from time import time; t = time()
from mathplus import first_factor, permutations, reduce

p = 0
for ps in permutations(range(7, 0, -1)):
    if ps[0] == 5: continue
    p = reduce(lambda x, y: 10*x+y, ps)
    if first_factor(p) == p:
        break

print(p)#, time()-t
예제 #12
0
    #* 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.

#Answer:
	#16695334890

from time import time; t=time()
from mathplus import permutations, reduce

def three(d, i):
    return d[i]*100+d[i+1]*10+d[i+2]

ret = []
for d in permutations(range(10)):
    if d[5] != 0 and d[5] != 5: continue
    if d[3] % 2 != 0: continue
    #if three(d, 1) % 2 != 0: continue
    if (d[2]+d[3]+d[4]) % 3 != 0: continue
    #if three(d, 2) % 3 != 0: continue
    #if three(d, 3) % 5 != 0: continue
    if (d[4]*2+d[5]*3+d[6]) % 7 != 0: continue
    #if three(d, 4) % 7 != 0: continue
    if (d[5]-d[6]+d[7]) % 11 != 0: continue
    #if three(d, 5) % 11 != 0: continue
    if (d[6]*9-d[7]*3+d[8]) % 13 != 0: continue
    #if three(d, 6) % 13 != 0: continue
    if (-2*d[7]-7*d[8]+d[9]) % 17 != 0: continue
    #if three(d, 7) % 17 != 0: continue
    ret.append(reduce(lambda x, y:10*x+y, d))
예제 #13
0
primes, sieves = get_primes_by_sieve(M)
def is_prime_plus(n):
    if n % 2 == 0: return n == 2
    if n % 3 == 0: return n == 3
    if n < M: return sieves[n]
    isqrtn = isqrt(n)
    for p in primes:
        if p > isqrtn: return True
        if n % p == 0: return False

ret = set()

def tryn(n, l, start_i, tr, maxn):
    for i in range(start_i, 9):
        n = n*10+l[i]
        if n < maxn: continue
        if is_prime_plus(n):
            tr.append(n)
            tryn(0, l, i+1, tr, n)
            tr.pop()
    if start_i == 9:
        ret.add(tuple(sorted(tr)))

for l in permutations(range(1, 10)):
    if l[-1] % 2 == 0 or l[-1] == 5: continue
    tr = []
    tryn(0, l, 0, tr, 0)

print(len(ret))#, time()-t
예제 #14
0
#We shall say that an n-digit number is pandigital if it makes use of all the digits 1 to n exactly once; for example, the 5-digit number, 15234, is 1 through 5 pandigital.

#The product 7254 is unusual, as the identity, 39  186 = 7254, containing multiplicand, multiplier, and product is 1 through 9 pandigital.

#Find the sum of all products whose multiplicand/multiplier/product identity can be written as a 1 through 9 pandigital.

#HINT: Some products can be obtained in more than one way so be sure to only include it once in your sum.

#Answer:
    #45228  

from time import time; t=time()
from mathplus import permutations, reduce

def to_int(l):
    return reduce(lambda x, y: x*10+y, l)

ret = set()
for i in permutations(range(1, 10), 9):
    if i[0] >= 8: break
    if i[0]*i[2] < 10 and i[1]*i[4] % 10 == i[8]:
        r = to_int(i[5:])
        if to_int(i[:2]) * to_int(i[2:5]) == r:
            ret.add(r)
    if i[0]*i[1] < 10 and i[0]*i[4] % 10 == i[8]:
        r = to_int(i[5:])
        if to_int(i[:1]) * to_int(i[1:5]) == r:
            ret.add(r)
print(sum(ret))#, time()-t
예제 #15
0
def direct_no_thinking(n, size):
    return list(permutations(range(size)))[n]
예제 #16
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?

#Answer:
	#296962999629

from time import time; t=time()
from mathplus import sieve, permutations

ret = {}
primes = sieve(10000)
p = [i for i, f in enumerate(primes) if f and i >= 1000]
for n in p:
    c = []
    for i in permutations((n//1000, (n % 1000)//100, (n % 100)//10, n%10)):
        if 0 in i: break
        m = i[0]*1000+i[1]*100+i[2]*10+i[3]
        if primes[m] and m not in c: c.append(m)
    if len(c) >= 3:
        c = sorted(c)
        if c[0] in ret: continue
        for i in range(1, len(c)-1):
            for j in range(i):
                if c[i]*2-c[j] in c:
                    ret[c[0]] = (c[j], c[i], 2*c[i]-c[j])
assert len(ret) == 2
del ret[1487]
print(['%s%s%s' %(v[0],v[1],v[2]) for k, v in ret.items()][0])#, time()-t