Пример #1
0
 def test(s):
     if full_f[s[0]] == 0: return 0
     c = full_f[s[-1]]
     if c in (0, 2, 3, 7, 8): return 0
     d = full_f[s[-2]]
     if c == 5 and d != 2: return 0
     if c == 6 and d % 2 == 0: return 0
     if d % 2 == 1: return 0
     n = reduce(lambda x,y: x*10+y, (full_f[c] for c in s))
     return n if isqrt(n)**2 == n else 0
Пример #2
0
 def test(s):
     if full_f[s[0]] == 0: return 0
     c = full_f[s[-1]]
     if c in (0, 2, 3, 7, 8): return 0
     d = full_f[s[-2]]
     if c == 5 and d != 2: return 0
     if c == 6 and d % 2 == 0: return 0
     if d % 2 == 1: return 0
     n = reduce(lambda x, y: x * 10 + y, (full_f[c] for c in s))
     return n if isqrt(n)**2 == n else 0
Пример #3
0
#0.123456789101112131415161718192021...

#It can be seen that the 12th digit of the fractional part is 1.

#If dn represents the nth digit of the fractional part, find the value of the following expression.

#d1 × d10 × d100 × d1000 × d10000 × d100000 × d1000000

#Answer:
	#210

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

def idf(n):
    n -= 1
    i = 1
    c = (10**i-10**(i-1))*i
    while n > c:
        n -= c
        i += 1
        c = (10**i-10**(i-1))*i
    u, v = n / i, n % i
    return int(str(10**(i-1)+u)[v])

d = [1, 10, 100, 1000, 10000, 100000, 1000000]
d = map(idf, d)
#print d
print(reduce(op.mul, d, 1))#, time()-t
Пример #4
0
def mul(l):
    return reduce(op.mul, l, 1)
Пример #5
0
#What is the least value of n for which the number of distinct solutions exceeds four million?

#NOTE: This problem is a much more difficult version of problem 108 and as it is well beyond the limitations of a brute force approach it requires a clever implementation.

#Answer:
	#9350130049860600

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

M = 4000000
L = M*2-1
S = 15#assert S == int(log(L)/log(3))+1 == len(primes)
primes = (2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47)

min_n = reduce(op.mul, primes, 1)

def get_next(pos, n, k, limit):
    global min_n
    if k >= L:
        if n < min_n: min_n = n
        return True
    #if pos == S: return False
    for p in range(1, limit+1):
        n *= primes[pos]
        if n > min_n: break
        if get_next(pos+1, n, k*(2*p+1), p): break
    return False

get_next(0, 1, 1, M)
Пример #6
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
Пример #7
0
#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))
print(sum(ret))  #, time()-t
Пример #8
0
#NOTE: This problem is a much more difficult version of problem 108 and as it is well beyond the limitations of a brute force approach it requires a clever implementation.

#Answer:
#9350130049860600

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

M = 4000000
L = M * 2 - 1
S = 15  #assert S == int(log(L)/log(3))+1 == len(primes)
primes = (2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47)

min_n = reduce(op.mul, primes, 1)


def get_next(pos, n, k, limit):
    global min_n
    if k >= L:
        if n < min_n: min_n = n
        return True
    #if pos == S: return False
    for p in range(1, limit + 1):
        n *= primes[pos]
        if n > min_n: break
        if get_next(pos + 1, n, k * (2 * p + 1), p): break
    return False

Пример #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
	#40824

from mathplus import op, reduce

CNT = 5
TABLE = '''
73167176531330624919225119674426574742355349194934
96983520312774506326239578318016984801869478851843
85861560789112949495459501737958331952853208805511
12540698747158523863050715693290963295227443043557
66896648950445244523161731856403098711121722383113
62229893423380308135336276614282806444486645238749
30358907296290491560440772390713810515859307960866
70172427121883998797908792274921901699720888093776
65727333001053367881220235421809751254540594752243
52584907711670556013604839586446706324415722155397
53697817977846174064955149290862569321978468622482
83972241375657056057490261407972968652414535100474
82166370484403199890008895243450658541227588666881
16427171479924442928230863465674813919123162824586
17866458359124566529476545682848912883142607690042
24219022671055626321111109370544217506941658960408
07198403850962455444362981230987879927244284909188
84580156166097919133875499200524063689912560717606
05886116467109405077541002256983155200055935729725
71636269561882670428252483600823257530420752963450
'''.replace('\n','')
TABLE = [int(i) for i in TABLE]

print(max(reduce(op.mul, TABLE[i:i+CNT], 1) for i in range(len(TABLE)-CNT+1)))
Пример #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
#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))
print(sum(ret))#, time()-t
Пример #13
0
def mul(l):
    return reduce(op.mul, l, 1)
Пример #14
0
def to_int(l):
    return reduce(lambda x, y: x*10+y, l)
Пример #15
0
#It can be seen that the 12th digit of the fractional part is 1.

#If dn represents the nth digit of the fractional part, find the value of the following expression.

#d1 × d10 × d100 × d1000 × d10000 × d100000 × d1000000

#Answer:
#210

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


def idf(n):
    n -= 1
    i = 1
    c = (10**i - 10**(i - 1)) * i
    while n > c:
        n -= c
        i += 1
        c = (10**i - 10**(i - 1)) * i
    u, v = n / i, n % i
    return int(str(10**(i - 1) + u)[v])


d = [1, 10, 100, 1000, 10000, 100000, 1000000]
d = map(idf, d)
#print d
print(reduce(op.mul, d, 1))  #, time()-t
Пример #16
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)