def findTruncatable(): primeCount = 0 primeSum = 0 # Guess at an upper bound primeList, primeSieve = slowPrimes(1000000) for p in primeList[4:]: digits = digitize(p) for i in xrange(1, len(digits)): num = undigitize(digits[i:]) if not primeSieve[num]: break num = undigitize(digits[:-1*i]) if not primeSieve[num]: break else: primeCount += 1 primeSum += p if primeCount == 11: break # Check that we reached 11 primes print(primeCount) return primeSum
def compute(nums, digitList, digitSet, answerList): """ Recursively find the answer. """ # Should be at a base case if not nums: if len(digitList) != 9: return answer = undigitize(list(DIGITS.difference(digitSet)) + digitList) answerList.append(answer) return checkNum = nums[-1] numStart = digitList[-2:] + [""] # Make the digits to check digitsToCheck = DIGITS.difference(digitSet) # Check all possible digits for newDigit in digitsToCheck: numStart[-1] = newDigit if undigitize(numStart) % checkNum: continue # Add to the digit list and digit set newDigitList = digitList + [newDigit] newDigitSet = digitSet.union(set((newDigit,))) # Recursively call compute compute(nums[:-1], newDigitList, newDigitSet, answerList)
def solve(): potentialPrimes = [] # Compute potential candidates (could be sped up by removing this) for p in primeList: possibleDigits = possible(p) if possibleDigits: potentialPrimes.append((p, possibleDigits)) # Loop through potential primes for p, digitList in potentialPrimes: digits = digitize(p) for d in digitList: # Count the size of the family familySize = 0 for i in xrange(10): # Replace the desired digits newDigits = listReplace(digits, d, i) newNum = undigitize(newDigits) # Make sure that we are not replacing the leading # number with a 0 if len(digitize(newNum)) < len(digits): continue if primeSieve[newNum]: familySize += 1 if familySize >= FAMILY_SIZE: return p # No answer found... return None
def rotate(n): ''' Rotate the number n. e.g. 123 -> 123, 231, 312 This is a generator. ''' digitList = digitize(n) for i in xrange(len(digitList)): yield undigitize(digitList[i:] + digitList[:i])
def isLychrel(n): ''' Determine if n is a "Lychrel" number. ''' for _ in range(0, 50): digits = euler.digitize(n) revN = euler.undigitize(digits[::-1]) n = revN + n if isPalindrome(n): return False return True
def dblPal(): ''' Find sum of all double palindromes (base 2 and 10) below 1000000. ''' palSum = 0 # Also do the 1 digit numbers for num in xrange(1, 10, 2): if isPalindrome(bin(num)[2:]): palSum += num # All palindromes that are 2, 3, 4, 5 digits in length for x in xrange(1, 100): digits = digitize(x) if digits[0] % 2 == 0: continue endDigits = digits[::-1] num = undigitize(digits + endDigits) if isPalindrome(bin(num)[2:]): palSum += num for d in xrange(10): num = undigitize(digits + [d] + endDigits) if isPalindrome(bin(num)[2:]): palSum += num # Also do the 6 digit numbers for x in xrange(100, 1000): digits = digitize(x) if digits[0] % 2 == 0: continue endDigits = digits[::-1] num = undigitize(digits + endDigits) if isPalindrome(bin(num)[2:]): palSum += num return palSum
def multiples(): ''' Find largest pandigital number formed as a concatenated product of an integer with (1, 2, ..., n) with n > 1 ''' maxProd = 918273645 # Check up to 4 digit numbers for i in xrange(9111, 10000): prod = digitize(i) if prod[0] != 9: continue p = 2 while(len(prod) < 9): prod += digitize(p * i) p += 1 if isPandigital(prod): maxProd = max(undigitize(prod), maxProd) return maxProd
from collections import Counter from euler import digitize, undigitize n = 100 while True: digits = digitize(n) digits.insert(0, 1) dCount = Counter(digits) newNum = undigitize(digits) for i in xrange(2, 7): if Counter(digitize(newNum*i)) != dCount: break else: print(newNum) break n += 1