Esempio n. 1
0
def findpancat(pan):
    rval = 0
    ipan = array2int(pan)
    # We only need to search for something < 5 digits
    for ii in range(1, 6):
        start = array2int(pan[0:ii])
        cprod = makecatprod(start, 9)
        if cprod == ipan:
            rval = 1
    return rval
Esempio n. 2
0
def findlargestpandigital():
    for n in range(9, 1, -1):
        pan = makepandigital(n, True)
        for p in pan:
            pint = array2int(p)
            # print "Testing: " + repr(pint)
            if isprime(pint):
                print "Found the largest pandigital prime: " + repr(pint)
                return pint
Esempio n. 3
0
def replaceprime(prime):
    findall = lambda my_list: [i for i, x in enumerate(my_list) if x == '1']
    ndig = len(int2array(prime))
    pgroup = []
    replacement_dig = make_palindrome_half(ndig-1)
    # Ignore the last replacement rule, it is '0000'
    for reprule in replacement_dig[:-1]:
        # Run replacement for all integers
        subgroup = []
        newprime = int2array(prime)
        for cint in range(10):
            for ind in findall(reprule):
                newprime[ind] = cint
            # Check that the length is the same
            if len(newprime) == len(int2array(array2int(newprime))):
                subgroup.append(array2int(newprime))
        pgroup.append(subgroup)
    return pgroup
Esempio n. 4
0
            # print "List: " + repr(nums)
            # print "Current sum: " + repr(cvalue)
            # print "Additional terms: " + repr(naddperms)
            # print "Target term: " + repr(lt)
            num = 0
            while num <= nleft:
                # print repr(num) + "  " + repr(nums[num-1]) +
                # "  " + repr(cvalue + (num)*naddperms)
                if cvalue + (num)*naddperms == lt:
                    nt = nums.pop(num-1)
                    mlt.append(nt)
                    nums.reverse()
                    mlt.extend(nums)
                    nums = []
                    nleft = 0
                    break
                elif cvalue + (num)*naddperms > lt:
                    cvalue += (num-1)*naddperms
                    nt = nums.pop(num-1)
                    mlt.append(nt)
                    break
                elif num == nleft:
                    nt = nums.pop(num)
                    mlt.append(nt)
                    break
                num += 1
            # print mlt
            nleft = len(nums)
        print repr(array2int(mlt)) + " is the " + repr(lt) +
        "th lexographic permutation of the set"
Esempio n. 5
0
    else:
        if len(num) == maxlen:
            rval = int(num)
    return rval


# Takes in a pandigital number and determines if it can
#  be formed by concatenating the product of an integer
# We do this by taking the first n digits, and multiplying
#  them by the sequence (1,2,3,4,...) until we have a
#  9 digit number, then we can compare that
def findpancat(pan):
    rval = 0
    ipan = array2int(pan)
    # We only need to search for something < 5 digits
    for ii in range(1, 6):
        start = array2int(pan[0:ii])
        cprod = makecatprod(start, 9)
        if cprod == ipan:
            rval = 1
    return rval

if __name__ == "__main__":
    plist = makepandigital(9, True)
    for pan in plist:
        findpancat(pan)
        if findpancat(pan):
            print ("The largest pandigital that can be formed " +
                   "as a concatenated product is: %d" % (array2int(pan)))
            break
Esempio n. 6
0
def truncateRL(num):
    arr = int2array(num)
    arr.pop()
    tnum = array2int(arr)
    return tnum
Esempio n. 7
0
#  The first is; 1487, 4817, 8147 (difference is 3330)
from eulermath import (isprime, int2array, array2int,
                       find_primes_in_list, find_arithemetric_series)
from itertools import permutations, combinations

if __name__ == "__main__":
    # Start by finding all 4-digit primes
    print "Finding arithemetric series of primes between 1001 and 9999:"
    exclude = []
    for trial in xrange(1001, 9999, 2):
        if trial not in exclude:
            if isprime(trial):
                # Find the permutations of this number
                tp = permutations(int2array(trial))
                # Get the unique values and sort them
                tpl = list(set([array2int(x) for x in tp]))
                tpp = find_primes_in_list(tpl)
                tpn = []
                for x in tpp:
                    if x > 1000:
                        tpn.append(x)
                # Exclude the others
                for p in tpn:
                    exclude.append(p)
                # Find any arithemetric series of length 3 in the prime list
                aseries = find_arithemetric_series(tpn, 3)
                if len(aseries) > 0:
                    print aseries
                    for series in aseries:
                        print series
                        cat = ''.join(str(elem) for elem in series)
Esempio n. 8
0
    p12 = []
    p23 = []
    p1 = []
    p2 = []
    p3 = []
    fnames = open('Euler79.dat')
    for line in fnames:
        val = int2array(int(line))
        # All values
        sub.append(val)
        # Single values
        p1.append(val[0])
        p2.append(val[1])
        p3.append(val[2])
        # Pairs
        p12.append(array2int(val[0:2]))
        p23.append(array2int(val[1:3]))
        p123.append(array2int(val))

    # Flatten out the sublist and take all unique elements
    fsl = list(set([x for arr in sub for x in arr]))
    # this length is the shortest password size

    v1, c1 = hist(p1)
    v2, c2 = hist(p2)
    v3, c3 = hist(p3)
    v12, c12 = hist(p12)
    v23, c23 = hist(p23)
    v123, c123 = hist(p123)

    print "We need to use all these:"