def main(argv): rna = fasta.read_one(argv[0]) au_max = max(rna.count('A'), rna.count('U')) au_min = min(rna.count('A'), rna.count('U')) cg_max = max(rna.count('C'), rna.count('G')) cg_min = min(rna.count('C'), rna.count('G')) print combinatorics.permutations( au_max, au_min) * combinatorics.permutations(cg_max, cg_min)
def euler_41(): "What is the largest n-digit pandigital prime that exists?" from combinatorics import permutations # Pan-digital primes are 4 or 7 digits. Others divisible by 3 prime._refresh(2766) # sqrt(7654321) x = list(range(7, 0, -1)) for perm in permutations(x): num = 0 for n in perm: num = num * 10 + n if prime._isprime(num): print("Lösung:", num) break
def euler_43(): "Find the sum of all pandigital numbers with an unusual sub-string divisibility property." from combinatorics import permutations def num(l): s = 0 for n in l: s = s * 10 + n return s def subdiv(l, n): return not num(l) % n total = 0 for perm in permutations((0,1,2,3,4,6,7,8,9)): perm.insert(5, 5) # d6 must be 5 if (subdiv(perm[7:10], 17) and subdiv(perm[6:9], 13) and subdiv(perm[5:8], 11) and subdiv(perm[4:7], 7) and subdiv(perm[3:6], 5) and subdiv(perm[2:5], 3) and subdiv(perm[1:4], 2)): total += num(perm) print("Lösung:", total)
def euler_49(): "Find arithmetic sequences, made of prime terms, whose four digits are permutations of each other." from combinatorics import permutations prime._refresh(10000) for num in range(1000, 10000): if str(num).find('0') >= 0: continue if prime.isprime(num): prime_permutations = { num: 1 } for x in permutations(list(str(num))): next_num = int(''.join(x)) if prime.isprime(next_num): prime_permutations[next_num] = 1 primes = sorted(prime_permutations.keys()) for a in range(0, len(primes)): if primes[a] == 1487: continue for b in range(a+1, len(primes)): c = (primes[a] + primes[b]) // 2 if c in prime_permutations: print("Lösung:", str(primes[a]) + str(c) + str(primes[b])) return
''' 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? ''' import prime from combinatorics import permutations # Pan-digital primes are 4 or 7 digits. Others divisible by 3 prime._refresh(2766) # sqrt(7654321) for perm in permutations(range(7, 0, -1)): num = 0 for n in perm: num = num * 10 + n if prime._isprime(num): print num break
from combinatorics import permutations def num(l): s = 0 for n in l: s = s * 10 + n return s product = {} for perm in permutations(range(1,10)): for cross in range(1,4): for eq in range(cross+1, 6): a = num(perm[0:cross]) b = num(perm[cross:eq]) c = num(perm[eq:9]) if a * b == c: product[c] = 1 print sum(p for p in product)
d5 d6 d7 = 357 is divisible by 7 d6 d7 d8 = 572 is divisible by 11 d7 d8 d9 = 728 is divisible by 13 d8 d9 d10= 289 is divisible by 17 Find the sum of all 0 to 9 pandigital numbers with this property. ''' from combinatorics import permutations def num(l): s = 0 for n in l: s = s * 10 + n return s def subdiv(l, n): return num(l) % n == 0 total = 0 for perm in permutations((0,1,2,3,4,6,7,8,9)): perm.insert(5, 5) # d6 must be 5 if (subdiv(perm[7:10], 17) and subdiv(perm[6:9], 13) and subdiv(perm[5:8], 11) and subdiv(perm[4:7], 7) and subdiv(perm[3:6], 5) and subdiv(perm[2:5], 3) and subdiv(perm[1:4], 2)): total += num(perm) print total
def test_permutations_of_n_options(): assert permutations(10) == 3628800
def test_permutations_of_non_numeric_throws_exception(): with pytest.raises(Exception): permutations([])
The arithmetic sequence, 1487, 4817, 8147, in which each of the terms increases by 3330, is unusual in two ways: (i) each of the three terms are prime, and, (ii) each of the 4-digit numbers are permutations of one another. 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 prime from combinatorics import permutations prime._refresh(10000) for num in xrange(1000, 10000): if str(num).find('0') >= 0: continue if prime.isprime(num): prime_permutations = { num: 1 } for x in permutations(list(str(num))): next_num = int(''.join(x)) if prime.isprime(next_num): prime_permutations[next_num] = 1 primes = sorted(prime_permutations.keys()) for a in xrange(0, len(primes)): if primes[a] == 1487: continue for b in xrange(a+1, len(primes)): c = (primes[a] + primes[b]) / 2 if prime_permutations.has_key(c): print str(primes[a]) + str(c) + str(primes[b]) exit()