def panDigitProducts( permList ): """Take each permutation of digits and slice into 3 pieces. Check to see if each piece is a proper product. """ for ai in range(1,9): for bi in range(ai): for perm in permList: i, j, k = perm[:ai], perm[ai:ai+bi], perm[ai+bi:] if number(i)*number(j) == number(k): yield number(k)
def substringDivisibility( i ): """The substring divisibility property. The number is represented as a sequence of digits. >>> from euler43 import substringDivisibility >>> substringDivisibility( digits(1406357289) ) True >>> substringDivisibility( digits(1406357298) ) False """ n2= number(i[1:4]) n3= number(i[2:5]) n5= number(i[3:6]) n7= number(i[4:7]) n11= number(i[5:8]) n13= number(i[6:9]) n17= number(i[7:10]) test = ( n2 % 2 == 0, n3 % 3 == 0, n5 % 5 == 0, n7 % 7 == 0, n11%11 == 0, n13%13 == 0, n17%17 == 0 ) return all( test )
def genPanDigitalProdConcat(): """Get the list of pandigital products. >>> from euler38 import genPanDigitalProdConcat >>> pdpc= list( genPanDigitalProdConcat() ) >>> pdpc.sort() >>> pdpc # doctest: +ELLIPSIS [123456789, 192384576, 219438657, ..., 927318546, 932718654] """ for i in range(1000000): seq= prodConcat(i) if pandigital(seq): yield number(seq)
def PDSD_gen(): """Generate pan-digital numbers with the substring divisibility property. The number is represented as a sequence of digits. >>> from euler43 import PDSD_gen >>> numbers= list( PDSD_gen() ) >>> 1406357289 in numbers True >>> numbers [1406357289, 1430952867, 1460357289, 4106357289, 4130952867, 4160357289] """ pandigital10= Permutation(range(10)) for i in pandigital10.nextPerm(): if substringDivisibility(i): yield number(i)
def reverseAndAdd( n ): """ >>> from euler55 import reverseAndAdd >>> from euler04 import palindrome >>> reverseAndAdd(47) 121 >>> palindrome( reverseAndAdd(47) ) True >>> reverseAndAdd( reverseAndAdd( reverseAndAdd( 349 ) ) ) 7337 >>> palindrome( reverseAndAdd( reverseAndAdd( reverseAndAdd( 349 ) ) ) ) True """ d = digits(n) d.reverse() return n + number(d)
def pandigitalPrimes(size): """Generate all pan-digital primes of a given size. >>> from euler41 import pandigitalPrimes >>> pd4 = list( pandigitalPrimes(4) ) >>> 2143 in pd4 True >>> pd4 [1423, 2143, 2341, 4231] """ permN= Permutation( range(1,size+1) ) for p in permN.nextPerm(): ld= p[-1] if ld % 2 == 0: continue # skip even numbers if ld == 5: continue # skip 5's, also n= number( p ) if isprime(n): yield n
def answer(): for a,b,c in primeGapPerm(): if a == 1487 and b == 4817 and c == 8147: continue return number( digits(a) + digits(b) + digits(c) )