def genNdigitNPower( limit=30 ): """ >>> s5= list( genNdigitNPower(6) ) >>> (7, 5, 16807) in s5 True >>> s9= list( genNdigitNPower(10) ) >>> (8, 9, 134217728) in s9 True """ for p in range(limit): i= 0 while len(digits(i**p)) <= p: if len(digits(i**p)) == p: yield i, p, i**p i += 1
def primeGapPerm(): """There are just two answers. >>> from euler49 import primeGapPerm >>> list(primeGapPerm()) [(1487, 4817, 8147), (2969, 6299, 9629)] """ p4 = list(makePrimes()) gaps= set( makeGaps(p4) ) step3= list( threeStep( p4, gaps ) ) for a, b, c in step3: da= digits(a) db= digits(b) dc= digits(c) if isPermutation(da,db) and isPermutation(db,dc): yield a,b,c
def genUnorthodoxCancels(): """Compare digit cancellation and proper fraction reduction. >>> from euler33 import genUnorthodoxCancels >>> list( genUnorthodoxCancels() ) [(16, 64), (26, 65), (19, 95), (49, 98)] """ for d in range(11,100): for n in range(11,d): dn, dd = digits(n), digits(d) if dn[1] == 0 or dd[1] == 0: continue n2, d2 = cancelDigits(dn,dd) if dn == n2: continue # No improper digit cancellation r= gcd(n,d) r2= gcd(n2[0],d2[0]) if n//r == n2[0]//r2 and d//r == d2[0]//r2: #print( n, d, n2, d2 ) yield n, d
def pow2_digits3( n ): """Return digits from :math:`2^n` >>> from euler16 import pow2_digits3 >>> pow2_digits3(15) [3, 2, 7, 6, 8] >>> sum(pow2_digits3(15)) 26 """ return digits(2**n)
def numerator( convergent, position ): """ >>> converge_e = fraction_steps( 2, tuple(den_pat_e(10)) ) >>> n= numerator( converge_e, 10 ) >>> n [1, 4, 5, 7] >>> sum( n ) 17 """ [ next(convergent) for i in range(position-1) ] term= next(convergent) #print( term, float(term) ) numerator = term.numerator return digits(numerator)
def digit_sum_gen(): """ >>> from euler56 import digit_sum_gen >>> max( digit_sum_gen() ) (972, 99, 95) >>> sum(digits(99**95)) 972 """ for a in range(100): p = 1 for b in range(100): s = sum(digits(p)) yield s, a, b p *= a
def prodConcat( n ): """Create a 9-digit concatenaed product by successive multiplications of n by 1, 2, 3, ..., 9 >>> from euler38 import prodConcat >>> prodConcat( 192 ) [1, 9, 2, 3, 8, 4, 5, 7, 6] >>> prodConcat( 9 ) [9, 1, 8, 2, 7, 3, 6, 4, 5] """ seq= [] for p in range(1,10): seq.extend( digits(n*p) ) if len(seq) >= 9: break return seq
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 digitStream( limit ): """ >>> from euler40 import digitStream >>> s= list(digitStream(32)) >>> s[11] 1 >>> s [1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 0, 1, 1, 1, 2, 1, 3, 1, 4, 1, 5, 1, 6, 1, 7, 1, 8, 1, 9, 2, 0, 2, 1] """ n= 1 dFed= 0 while dFed < limit: for d in digits(n): yield d dFed += 1 n += 1
def circularPrimes( limit ): """ >>> from euler35 import circularPrimes >>> cp100= circularPrimes( 100 ) >>> len(cp100) 13 >>> sorted( set( cp100 ) ) [2, 3, 5, 7, 11, 13, 17, 31, 37, 71, 73, 79, 97] """ cp= set() for n in range(2,limit): if isprime(n) and n not in cp: rs= [ number(r) for r in rotations( digits(n) ) ] circular= all( map( isprime, rs ) ) if circular: for r in rs: cp.add(r) return cp
def answer(): for i in range(1,1000000000): di= digits(i) p = [ isPermutation( di, digits(k*i) ) for k in range(2,7) ] if all(p): return i
def curious( limit=999999 ): for i in range(3,limit+1): p = sum( map( fact, digits(i) ) ) #print i, p if p == i: yield i
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) )
def test(): import doctest doctest.testmod(verbose=0) assert pandigital( digits(192384576) )