Example #1
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
Example #2
0
def isPp2Sq(num):
    i = 1
    rval = 0
    while 2*(i**2) < num:
        res = num - 2*(i**2)
        if isprime(res):
            # We can do the decomposition
            rval = 1
            break
        i += 1
    return rval
Example #3
0
def istruncprime(n):
    istp = 0
    if (n > 10 and isprime(n)):
        istplr = 1
        istprl = 1
        # if we have two digits we can pop another off
        nt = n
        while nt > 10:
            nt = truncateLR(nt)
            # print "nt: " + repr(nt) + "  prime: " + repr(isprime(nt))
            if (isprime(nt) == 0):
                istplr = 0
                break
        if (istplr == 1):
            nt = n
            while nt > 10:
                nt = truncateRL(nt)
                # print "nt: " + repr(nt) + "  prime: " + repr(isprime(nt))
                if (isprime(nt) == 0):
                    istprl = 0
                    break
        if (istplr and istprl):
            istp = 1
    return istp
Example #4
0
def primefactors(n):
    pf = []
    while not isprime(n):
        founddiv = False
        pi = 1
        while not founddiv:
            pn = nthprime(pi)
            # This prime evenly divides, factor it out
            if n % pn == 0:
                n = n/pn
                pf.append(pn)
                founddiv = True
            pi += 1

    # Note: we have also reduced n down to a prime, include it as well
    pf.append(n)
    return pf
Example #5
0
from eulermath import isprime

if __name__ == "__main__":
    # Start looking for primes at 3 - skip all the
    # evens because they are not prime
    # test case, the 10th prime is 29
    # findpri = 10
    findpri = 10001
    indpri = 2   # The number of the prime we are - last found was 2
    cint = 3     # Current integer to test for prime-ness
    while (indpri < findpri):
        cint += 2
        # print "testing", cint, isprime(cint)
        if isprime(cint) == 1:
            indpri += 1

    print "The", indpri, "th prime is:", cint
Example #6
0
from eulermath import isprime
from eulermath import intcpermute

# print intcpermute(123)
# print isprime(4)

if __name__ == "__main__":
    # nmax = 100 # Test case, shoud give 13
    nmax = 1000000

    n = 2
    ncpri = 0
    prilist = []
    while n < nmax:
        if n % 100000 == 0:
            print "+10%"
        if isprime(n):
            for x in intcpermute(n):
                if isprime(x) != 1:
                    break
            else:
                ncpri += 1
                prilist.append(n)
        n += 1

    print "There are:", ncpri, "circular primes below", nmax
    # print prilist
Example #7
0
# Find the one other sequence of 4-digit permuted increasing primes
#  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
Example #8
0
from eulermath import isprime

if __name__ == "__main__":
    # Test case, should give 17
    # nmax = 10
    nmax = 2000000
    total = 2
    n = 3
    while (n < nmax):
        if (isprime(n) == 1):
            total += n
        n += 2

    print "The sum of primes below", nmax, "is", total
Example #9
0
if __name__ == "__main__":
    maxp = 10**6
    # Import a list of the primes < 1M
    prime_sm = []
    pfile = open("eplist.dat")
    for line in pfile:
        prime = int(line)
        if prime >= maxp:
            break
        else:
            prime_sm.append(prime)

    maxsum = 2
    maxlen = 1
    maxseq = []
    for jj in range(len(prime_sm)):
        for ii in range(jj):
            seq = prime_sm[ii:jj]
            seqsum = sum(seq)
            if (seqsum < maxp):
                if (isprime(seqsum)) and (len(seq) > maxlen):
                    maxsum = seqsum
                    maxlen = len(seq)
                    maxseq = seq
            else:   # we have exceeded the 1M mark
                break

    print maxsum
    print maxlen
    print maxseq
Example #10
0
import sys
from eulermath import isprime

if __name__ == "__main__":
    # n = 10
    # n = 13195 #should give 29
    n = 600851475143
    x = 2
    while x < n / 2:
        # for x in range(n-1,n/2,-1):
        # print "is", x, "a factor?"
        if n % x == 0:
            # This is a factor check if it is prime
            b = n / x
            c = isprime(b)
            print "testing number:", b, c
            if c == 1:
                # We are a prime number
                # Since we are going from large numbers
                #   To small, this must be the largst prime
                print "The largest prime factor of", n, "is", b
                break
        x += 1