示例#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 f(n):
    ret = {}
    if n == 2:
        for i, j in product(range(10), range(10)):
            ret[i, j] = int(i > 0 and i + j <= 9)
        return ret
    for (k, i), v in f(n - 1).items():
        for j in range(10 - i - k):
            ret.setdefault((i, j), 0)
            ret[i, j] += v
    return ret
示例#3
0
def f(n):
    ret = {}
    if n == 2:
        for i, j in product(range(10), range(10)):
            ret[i, j] = int(i>0 and i+j<=9)
        return ret
    for (k, i), v in f(n-1).items():
        for j in range(10-i-k):
            ret.setdefault((i, j), 0)
            ret[i, j] += v
    return ret
示例#4
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
示例#5
0
def f(l):
    for n in range(10000, M):
        if not sieves[n]: continue
        s = str(n)
        for i in range(10):
            si = strn[i]
            sc = s.count(si)
            if sc <= 1: continue
            replace_pos = [0]*sc
            for i in range(sc):
                replace_pos[i] = s.find(si, replace_pos[i-1]+1 if i > 0 else 0)
            start_j = 1 if replace_pos[0] == 0 else 0
            for it in product(*[(0,1)]*sc):
                if sum(it) < 2: continue
                c = 0
                r = list(s)
                for j in range(start_j, 10):
                    u = strn[j]
                    for k in range(sc):
                        if it[k] == 1: r[replace_pos[k]] = u
                    ret = int(''.join(r))
                    if sieves[ret]: c += 1
                if c >= l: return n
示例#6
0
#where |n| is the modulus/absolute value of n
#e.g. |11| = 11 and |−4| = 4

#Find the product of the coefficients, a and b, for the quadratic expression that produces the maximum number of primes for consecutive values of n, starting with n = 0.

#Answer:
#-59231

from time import time
t = time()
from mathplus import sieve, product

M = 15000
primes = sieve(M)
# one of (1+a+b, b*b+a*b+b) is not a prime, so n*n+a*n+b has less than range(b) primes
ab = [[a, b, b] for a, b in product(range(
    -1000 + 1, 1000, 2), [i for i in range(83, 1000, 2) if primes[i]])]

n = 0
lastone = None
while len(ab) > 1:
    lastone = ab[0]
    x = 2 * n + 1
    for v in ab:
        v[2] += x + v[0]
    ab = [[a, b, x] for a, b, x in ab if x > 0 and (x >= M or primes[x])]
    n += 1

lastone = ab[0]
print(lastone[0] * lastone[1])  #, time()-t
示例#7
0
#!/usr/bin/python
# -*- coding: utf-8 -*-

#A bag contains one red disc and one blue disc. In a game of chance a player takes a disc at random and its colour is noted. After each turn the disc is returned to the bag, an extra red disc is added, and another disc is taken at random.

#The player pays £1 to play and wins if they have taken more blue discs than red discs at the end of the game.

#If the game is played for four turns, the probability of a player winning is exactly 11/120, and so the maximum prize fund the banker should allocate for winning in this game would be £10 before they would expect to incur a loss. Note that any payout will be a whole number of pounds and also includes the original £1 paid to play the game, so in the example given the player actually wins £9.

#Find the maximum prize fund that should be allocated to a single game in which fifteen turns are played.

#Answer:
	#2269

from time import time; t=time()
from mathplus import factorial, product, reduce, op

N = 15
s = 0
for l in product(*([(0, 1)]*N)):
    if sum(l) * 2 > N:
        s += reduce(op.mul,
                ((i + 1) if v == 0 else 1 for i, v in enumerate(l)),
                1)

print(factorial(N+1)//s)#, time()-t)
示例#8
0
#NOTE: This problem is related to problems 103 and 106.

#Answer:
	#73702

from time import time; t=time()
from mathplus import product

DATA = [[int(i) for i in s.split(',')] for s in open('105-sets.txt').read().splitlines()]

s = 0
for data in DATA:
    l = sorted(data)
    lenl = len(l)
    flag = False
    for i in range(lenl-1):
        if l[i] == l[i+1]:
            flag = True
            break
    if flag: continue
    for k in range(1, lenl):
        if sum(l[-k:]) >= sum(l[:k+1]): break
    else:
        ss = set()
        for k in product(*([(0,1)]*lenl)):
            ss.add(sum(k[i]*l[i] for i in range(lenl)))
        if len(ss) == 2**lenl: s += sum(l)

print(s)#, time()-t
示例#9
0
#!/usr/bin/python
# -*- coding: utf-8 -*-

#A bag contains one red disc and one blue disc. In a game of chance a player takes a disc at random and its colour is noted. After each turn the disc is returned to the bag, an extra red disc is added, and another disc is taken at random.

#The player pays £1 to play and wins if they have taken more blue discs than red discs at the end of the game.

#If the game is played for four turns, the probability of a player winning is exactly 11/120, and so the maximum prize fund the banker should allocate for winning in this game would be £10 before they would expect to incur a loss. Note that any payout will be a whole number of pounds and also includes the original £1 paid to play the game, so in the example given the player actually wins £9.

#Find the maximum prize fund that should be allocated to a single game in which fifteen turns are played.

#Answer:
#2269

from time import time
t = time()
from mathplus import factorial, product, reduce, op

N = 15
s = 0
for l in product(*([(0, 1)] * N)):
    if sum(l) * 2 > N:
        s += reduce(op.mul,
                    ((i + 1) if v == 0 else 1 for i, v in enumerate(l)), 1)

print(factorial(N + 1) // s)  #, time()-t)
示例#10
0
    #where |n| is the modulus/absolute value of n
    #e.g. |11| = 11 and |−4| = 4

#Find the product of the coefficients, a and b, for the quadratic expression that produces the maximum number of primes for consecutive values of n, starting with n = 0.

#Answer:
	#-59231

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

M = 15000
primes = sieve(M)
# one of (1+a+b, b*b+a*b+b) is not a prime, so n*n+a*n+b has less than range(b) primes
ab = [[a, b, b] for a, b in product(
    range(-1000+1, 1000, 2),
    [i for i in range(83, 1000, 2) if primes[i]]
    )]

n = 0
lastone = None
while len(ab) > 1:
    lastone = ab[0]
    x = 2*n+1
    for v in ab:
        v[2] += x+v[0]
    ab = [[a, b, x] for a, b, x in ab if x > 0 and (x >= M or primes[x])]
    n += 1

lastone = ab[0]
print(lastone[0]*lastone[1])#, time()-t