Esempio n. 1
0
def genPerms():
    """Create the 9! permutations of the digits 1-9.

    >>> from euler32 import genPerms
    >>> permList= list(genPerms())
    >>> len(permList) # 9! = 362880
    362880
    >>> permList[:2]
    [[1, 2, 3, 4, 5, 6, 7, 8, 9], [1, 2, 3, 4, 5, 6, 7, 9, 8]]
    >>> permList[-2:]
    [[9, 8, 7, 6, 5, 4, 3, 1, 2], [9, 8, 7, 6, 5, 4, 3, 2, 1]]
    """
    permN= Permutation( range(1,10) ) # 1 to 9
    return permN.nextPerm()
Esempio n. 2
0
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)
Esempio n. 3
0
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