def main(): products = set() digits = (1, 2, 3, 4, 5, 6, 7, 8, 9) # The factors must either be 1, 4 or 2, 3 digits. # skip 1 (1 x N == N) for a in digits[1:]: for b in permutations([d for d in digits if d != a], 4): b = b[0] * 1000 + b[1] * 100 + b[2] * 10 + b[3] prod = a * b if prod > 9876: break if is_pandigital(a * 10**8 + b * 10**4 + prod): products.add(prod) for perm in permutations(digits, 2): a = perm[0] * 10 + perm[1] for b in permutations([d for d in digits if d not in perm], 3): b = b[0] * 100 + b[1] * 10 + b[2] prod = a * b if prod > 9876: break if is_pandigital(a * 10**7 + b * 10**4 + prod): products.add(prod) return sum(products)
def main(): products = set() digits = (1, 2, 3, 4, 5, 6, 7, 8, 9) # The factors must either be 1, 4 or 2, 3 digits. # skip 1 (1 x N == N) for a in digits[1:]: for b in permutations([d for d in digits if d != a], 4): b = b[0]*1000 + b[1]*100 + b[2]*10 + b[3] prod = a*b if prod > 9876: break if is_pandigital(a*10**8 + b*10**4 + prod): products.add(prod) for perm in permutations(digits, 2): a = perm[0]*10 + perm[1] for b in permutations([d for d in digits if d not in perm], 3): b = b[0]*100 + b[1]*10 + b[2] prod = a*b if prod > 9876: break if is_pandigital(a*10**7 + b*10**4 + prod): products.add(prod) return sum(products)
def main(): a, b, cnt = 1, 1, 2 # https://en.wikipedia.org/wiki/Fibonacci_number#Relation_to_the_golden_ratio φ = (1 + sqrt(5)) / 2 while True: # keep only the last 9 digits of the fibonacci numbers a, b = b, (a + b) % 10**9 cnt += 1 if is_pandigital(b): # use some logarithm rules num_digits = cnt*log10(φ) - log10(sqrt(5)) if is_pandigital(int(10 ** (num_digits % 1 + 8))): return cnt
def main(): a, b, cnt = 1, 1, 2 # https://en.wikipedia.org/wiki/Fibonacci_number#Relation_to_the_golden_ratio φ = (1 + sqrt(5)) / 2 while True: # keep only the last 9 digits of the fibonacci numbers a, b = b, (a + b) % 10**9 cnt += 1 if is_pandigital(b): # use some logarithm rules num_digits = cnt * log10(φ) - log10(sqrt(5)) if is_pandigital(int(10**(num_digits % 1 + 8))): return cnt
def pandigital_multiples(): for base in range(1, 99999): l = len(str(base)) for n in range(2, 9 // l + 1): result = ''.join(str(base * i) for i in range(1, n + 1)) if is_pandigital(result): yield result
def euler41(): #there are no 8 or 9 digit pandigital primes, as the sum of the digits wil #always be a multiple of 9 #search backwards to save time for pr in reversed(list(primes_gen(10000000))): if is_pandigital(pr): return pr
def main(): print 'Task 38' max_pan = 0 for i in xrange(1, 100000): for count in xrange(1, 100): prod = get_concatenated_product(i, count) if utils.is_pandigital(prod) and prod > max_pan: max_pan = prod print max_pan
def euler104(): #http://bit.ly/11cebzg rt5 = math.sqrt(5) phi = (1 + rt5) / 2 def fib_first_digits(n, digits=9): """n is the n-th fib number""" x = n * math.log10(phi) - math.log10(rt5) + 1 return int(pow(10, x - int(x) + digits - 1)) tmp = 10 ** 10 a, b, i = 0, 1, 1 while 1: a, b = b % tmp, (a + b) % tmp if is_pandigital(a, 9): if is_pandigital(fib_first_digits(i)): return i i += 1
def panprim(): pnd_prms = [] for i in range(5, 9): for num in [ int(''.join(p)) for p in itertools.permutations('123456789', i) ]: if utils.is_prime_faster(num) and utils.is_pandigital(num): pnd_prms.append(num) print(max(pnd_prms))
def main(): # The number to beat is 918273645. # The starting number can't be in the form 9X or 9XX, as that would # produce too few or too many digits. Thus, it must be in the form 9XXX. for digits in permutations("987654321", 4): start = "".join(digits) prod = start + str(int(start) * 2) if is_pandigital(int(prod)): return prod
def main(): products = set() for a in range(2, 3000): for b in range(2, 3000): p = a*b pand = is_pandigital(str(a) + str(b) + str(p), 9) if pand: products.add(p) elif pand is None: break return sum(products)
def euler38(): res = {"pandigital": 0, "n": 0} for n in xrange(2, 99999999): all_str = str(n) if all_str[0] == "9": for i in xrange(2, 10): if len(all_str) >= 9: break all_str += str(n * i) #pandigital check if len(all_str) == 9: all_str = int(all_str) if is_pandigital(all_str): if int(all_str) > res["pandigital"]: res = {"pandigital": all_str, "n": n} return res["pandigital"]
def euler32(): ret = 0 #only up to 9999 because there won't be enough digits to go round for i in xrange(10, 10000): istr = str(i) #don't bother if there are digit repeats if len(set(istr)) != len(istr): continue for a, b in multiplicands(i): combined = "%s%s%s" % (a, b, i) if len(combined) == 9 and "0" not in combined and \ is_pandigital(combined): ret += i break return ret
def problem_32(): # Build up the list of product candidates. We know that the total number of digits across all # solutions must be 9 digits, so we can exclude: # # a * b = cdefghi -- since the max we can get is 9 * 8, which doesn't have 7 digits # a * bc = defghi -- since the max we can get is 98 * 7, which doesn't have 6 digits # ab * cd = efghi -- since the max we can get is 98 * 76, which doesn't have 5 digits # etc # # We can limit to the following forms, since these are the only ways to get 9 digits total: # a * bcde = fghi # ab * cde = fghi # products = list() products.extend([(a, b, a*b) for a in range(1,10) for b in range(1000,10000)]) products.extend([(a, b, a*b) for a in range(10,100) for b in range(100,1000)]) pandigital_products = {c for a, b, c in products if is_pandigital('{}{}{}'.format(a,b,c))} print(sum(pandigital_products))
def problem_032(n: int = 9) -> int: """ Collect all unique suitable numbers up to upper bound and sum them all. - O(n^2) time-complexity - O(1) space-complexity """ unique_pandigital = set() upper_bound = find_upper_bound(n) for a in range(1, upper_bound): for b in range(1, upper_bound): prod = a * b concat = f'{a}{b}{prod}' if len( concat ) != n: # filter short/long multiplicand/multiplier/product identity continue if is_pandigital(int(concat)): unique_pandigital.add(prod) return sum(unique_pandigital)
def test_is_pandigital(self): self.assertTrue(is_pandigital(923456781)) self.assertFalse(is_pandigital(12345678)) self.assertFalse(is_pandigital(102345678)) self.assertFalse(is_pandigital(1023456789))
# Problem 41 from utils import is_prime from utils import is_pandigital print(max(i for i in range(7654321) if is_pandigital(i) and is_prime(i))) # Some observations about pandigital numbers: # if they have 9 digits, then they are divisible by 3 # if they have 8 digits, then they are divisble by 3. # biggest 7 digit pan number = 7654321
from utils import is_pandigital answers = [] for i in range(10000): s = "" for j in range(1, 20): k = j * i s += str(k) if len(s) > 8: if len(s) == 9: if is_pandigital(s): answers.append(int(s)) else: continue print max(answers)
''' Find the largest pandigital prime. The sum of 8 and 9-digit pandigital numbers divides by three, so all the numbers do too and none are prime. Start with the biggest pandigital 7-digit number and work down. You could also create the pandigital numbers using combinatorics. ''' from utils import is_pandigital, is_prime i = 7654321 while True: if is_pandigital(i): if is_prime(i): break i -= 2 print i
# Therefore: # 7. |z| = 4 # I.e. z = 4 combinations should only be considered results = set() # Further note that we can skip 100, 1000 etc because these # have repeating digits and include 0 # start from and end at numbers with unique digits # Case 1: |x|=3 |y|=2 |z|=4 for x in xrange(123, 987 + 1): for y in xrange(12, 98 + 1): z = x * y if is_pandigital(x, y, z): results.add(z) # Case 2: |x|=4 |y|=1 |z|=4 for x in xrange(1234, 9876 + 1): for y in xrange(1, 12 + 1): z = x * y if is_pandigital(x, y, z): results.add(z) runtime = time.time() - t0 print results print 'Results = {}'.format(sum(results)) print 'Runtime = {}'.format(runtime)
def is_nine_pandigital_product(a, b): my_str = str(a) + str(b) + str(a * b) if len(my_str) == 9 and is_pandigital(my_str): return True else: return False
at first sign of it being pandigital, concatenate and compare the summed values hints: has to be greater than 918273645 """ def testableInts(): """generator for all the probable integers. must start with 9""" yield 9 for n in range(91, 99): yield n for n in range(912, 988): yield n for n in range(9123, 9877): yield n for n in range(91234, 98766): yield n maxNum = 0 for x in testableInts(): num, i = [], 1 while len(num) < 9: num = num + utils.split(x * i) i += 1 if utils.is_pandigital(num): maxNum = max([utils.join(num), maxNum]) print(maxNum)
from utils import is_pandigital, is_prime_naive, primes_up_to # note that 3, 8 and 9-digit pandigitals are all divisible by 3 # so biggest is 7 digits at most primes = primes_up_to(7654321 + 1) print(max(p for p in primes for s in (str(p), ) if is_pandigital(s, len(s))))
# Therefore: # 7. |z| = 4 # I.e. z = 4 combinations should only be considered results = set() # Further note that we can skip 100, 1000 etc because these # have repeating digits and include 0 # start from and end at numbers with unique digits # Case 1: |x|=3 |y|=2 |z|=4 for x in xrange(123, 987 + 1): for y in xrange(12, 98 + 1): z = x * y if is_pandigital(x, y, z): results.add(z) # Case 2: |x|=4 |y|=1 |z|=4 for x in xrange(1234, 9876 + 1): for y in xrange(1, 12 + 1): z = x * y if is_pandigital(x, y, z): results.add(z) runtime = time.time() - t0 print results print 'Results = {}'.format(sum(results)) print 'Runtime = {}'.format(runtime)
def test_is_pandigital(self): self.assertTrue(is_pandigital(923456781)) self.assertFalse(is_pandigital(12345678)) self.assertFalse(is_pandigital(102345678)) self.assertFalse(is_pandigital(1023456789))