def main(): numbers = [ str(i) for i in range((100 // 17 + 1) * 17, 1000, 17) if euler.is_pandigital(i) ] for i, prime in enumerate((13, 11, 7, 5, 3, 2, 1)): numbers = [str(n) + number for n in range(10) for number in numbers] numbers = [ number for number in numbers if (int(number) // 10**(i + 1)) % prime == 0 and euler.is_pandigital(number) ] return sum(int(number) for number in numbers)
def compute(): candidates = list(prime_sieve(7654321)) ''' Pandigital numbers with 8 or 9 digits are always divisible by 3 so the solution can have at most 7 digits ''' for i in range(len(candidates)-1,1,-1): n = candidates[i] if is_pandigital(n): return n
def compute(): candidates=[] for n in range(2,10): m = 1 while len(str(concatenated_product(m,n))) < 10: m += 1 if len(str(concatenated_product(m,n))) == 9: candidates.append(concatenated_product(m,n)) candidates = list(filter(lambda x: is_pandigital(x), candidates)) return max(candidates)
def compute(): count = 0 products = set() for x in range(10000): for y in range(10000): z = x * y if z > 9999: break str_xyz = "".join(sorted(list(str(x) + str(y) + str(z)))) if is_pandigital(str_xyz) and len(str_xyz) == 9: count += 1 products.add(z) return sum(products)
#!/usr/bin/python -tt ''' problem 104: http://projecteuler.net/problem=104 cheating from :-( : http://blog.dreamshire.com/2009/06/04/project-euler-problem-104-solution/ NOTE: my call to str(int(fin(n))) turned out to grow linearly as the string representing fib(n) gre so I cheated. the implementation below use a closed for calculation to get the top 10 digits and a %10**9 truncated calculation for the lower 10 digits. Very clever. ''' from euler import is_pandigital def top_digits(n): ''' Use: http://en.wikipedia.org/wiki/Fibonacci_number#Closed-form_expression to calculate an aproximation of the Nth Fibonacci number. For the top digits, the approximation is good enough. ''' t = n * 0.20898764024997873 + (-0.3494850021680094) return int(10**(t - int(t) + 8)) n, f0, f1 = 2, 1, 1 while not is_pandigital(f1) or not is_pandigital(top_digits(n)): f0, f1 = f1, (f1+f0)%10**9 n += 1 print "Answer to PE104 = ", n
def euler38(): print( max( int(concatenated_product(x, 2)) for x in range(9123, 9876) if is_pandigital(concatenated_product(x, 2))))
def euler32(): print(sum(set(a * b for a in range(2, 60) for b in range(1234 if a < 10 else 123, 10000 // a) if is_pandigital(str(a) + str(b) + str(a * b)))))
def euler41(): print(max(p for p in sieve.primerange(2, 7654321) if is_pandigital(p)))
from euler import is_pandigital DIGITS = 9 max_pandigital = '' RANGE_LIMIT = 10**(DIGITS/3+1) quit_flag = False for a in reversed(xrange(1, RANGE_LIMIT)): for b in xrange(1, RANGE_LIMIT): result = a * b max_pandigital += str(result) if len(max_pandigital) > DIGITS: max_pandigital = '' break if max_pandigital.find('0') != -1: max_pandigital = '' break if is_pandigital(max_pandigital): quit_flag = True break if quit_flag: break print max_pandigital
def findnext(n): x, i = str(n), 2 while len(x) < 9: x += str(n * i) i += 1 return x if euler.is_pandigital(x) else 0
from itertools import product from euler import is_pandigital one_four_four = [(a * b, str(a) + str(b) + str(a * b)) for a, b in product(range(1, 10), range(1000, 10000)) if len(str(a * b)) == 4] two_three_four = [(a * b, str(a) + str(b) + str(a * b)) for a, b in product(range(10, 100), range(100, 1000)) if len(str(a * b)) == 4] all_products = one_four_four + two_three_four unique_pandigitals = { product for (product, mmp) in all_products if is_pandigital(mmp, 9) } print(sum(unique_pandigitals)) # 45228
def euler32(): print( sum( set(a * b for a in range(2, 60) for b in range(1234 if a < 10 else 123, 10000 // a) if is_pandigital(str(a) + str(b) + str(a * b)))))
#!/usr/bin/python # coding: UTF-8 """ @author: CaiKnife Pandigital mutiples Problem 38 Take the number 192 and multiply it by each of 1, 2, and 3: 192 1 = 192 192 2 = 384 192 3 = 576 By concatenating each product we get the 1 to 9 pandigital, 192384576. We will call 192384576 the concatenated product of 192 and (1,2,3) The same can be achieved by starting with 9 and multiplying by 1, 2, 3, 4, and 5, giving the pandigital, 918273645, which is the concatenated product of 9 and (1,2,3,4,5). What is the largest 1 to 9 pandigital 9-digit number that can be formed as the concatenated product of an integer with (1,2, ... , n) where n 1? """ from euler import is_pandigital def concatenate_product(n, i): return int("".join([str(n * x) for x in range(1, i + 1)])) print max([concatenate_product(n, i) for n in range(1, 10000) for i in range(1, 10) if is_pandigital(concatenate_product(n, i))])
def findnext(n): x, i = str(n), 2 while len(x) < 9: x += str(n*i) i += 1 return x if euler.is_pandigital(x) else 0
from euler import is_pandigital DIGITS = 9 products = [] RANGE_LIMIT = 10**(DIGITS/3+1) b_cache = {} for a in xrange(1, RANGE_LIMIT): for b in xrange(1, int(RANGE_LIMIT/a)): result = a * b number_str = str(a) + str(b) + str(result) if len(number_str) > DIGITS: continue if number_str.find('0') != -1: continue if is_pandigital(number_str): if a in b_cache: break b_cache[b] = True # print a, b, result products.append(result) else: continue break print sum(set(products))
from euler import is_prime, is_pandigital ''' any integer is divisible by 3 or 9 whose sum of digits is divisible by 3 or 9; therefore composite and not prime. 9+8+7+6+5+4+3+2+1 = 45, 8+7+6+5+4+3+2+1 = 36, 6+5+4+3+2+1 = 21, and 5+4+3+2+1 = 15, all of which are divisible by 3 and therefore could not yield a 1 to {5, 6, 8, 9} pandigital prime. So that leaves 4 and 7 digit prime numbers less than 4321 and 7654321 respectively. ''' n = 7654321 while not(is_prime(n) and is_pandigital(str(n), 7)): n -= 2 print n
def euler38(): print(max(int(concatenated_product(x, 2)) for x in range(9123, 9876) if is_pandigital(concatenated_product(x, 2))))
#!/usr/bin/python # coding: UTF-8 """ @author: CaiKnife Pandigital products Problem 32 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. """ from euler import is_pandigital pan_digits = [] for a in range(1, 5000): for b in range(1, 100): c = a * b if is_pandigital(a, b, c, length=9) and c not in pan_digits: pan_digits.append(c) print sum(pan_digits)
import euler # all other digit conbinatons except (1 .. 4) and (1 ... 7) can be divided evenly by 3 for i in reversed(euler.primes(2, 7654321)): if euler.is_pandigital(i, 1, 7): print('largest pandigital prime is %d' % i) break
# coding=utf-8 ''' Problem 32 06 December 2002 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. ''' import euler pandigitals = {} for i in range(1, 9876): for j in range(1, 9876): identity = str(i) + str(j) + str(i*j) if len(identity) > 9: break if (len(identity) == 9) and (euler.is_pandigital(identity)): pandigitals[i*j] = True print sum(list(pandigitals.keys()))