예제 #1
0
def findPrimesWithRepeats_old(upper_bound):
    ps = primes_pp.primeSeive(upper_bound)
    prime_list = ps.getPrimeList()

    for p in reversed(prime_list):
        if not hasDuplicate(p):
            prime_list.remove(p)
    return prime_list
예제 #2
0
def findPrimesWithRepeats(upper_bound):
    ps = primes_pp.primeSeive(upper_bound)
    prime_list = [p for p in ps.getPrimeList() if hasDuplicate(p)]

    # for p in reversed(prime_list):
    #     if not hasDuplicate(p):
    #         prime_list.remove(p)
    return prime_list
예제 #3
0
def findTruncatablePrimes(upper_limit):
    prime_seive = primes_pp.primeSeive(upper_limit)
    truncatable_primes = []
    for i in xrange(10, upper_limit):
        # print 'i: %d' % i
        if checkPrimesFromRight(i, prime_seive) and checkPrimesFromLeft(i, prime_seive):
            truncatable_primes.append(i)
    return truncatable_primes
예제 #4
0
def main():
    prime_sieve = primes_pp.primeSeive(1000)
    print '-------------------------------------'
    print testWithinGroup(1, 0.02, prime_sieve)
    print '-------------------------------------'
    print testWithinGroup(2, 0.02, prime_sieve)
    print '-------------------------------------'
    print testWithinGroup(3, 0.02, prime_sieve)
    print '-------------------------------------'
    print testWithinGroup(4, 0.02, prime_sieve)
    print '-------------------------------------'
    print testWithinGroup(5, 0.02, prime_sieve)
    print '-------------------------------------'
    print testWithinGroup(25, 0.02, prime_sieve)
예제 #5
0
def findLargestPrime():
    print 'getting prime seive'
    prime_seive = primes_pp.primeSeive(1e7)
    print 'prime seive constructed'

    max_prime = 0
    # definitely no 2,8, or 9 digit pandigital primes, so don't check
    for i in xrange(int(1e7),2,-1):
        # max_n_digit_prime = findLargestNDigitPrime(n_digit, prime_seive)
        if prime_seive.isPrime(i):
            if isPandigital(i):
                max_prime = i
                break

    print 'the max n-digit prime is %d' % max_prime
    return max_prime
예제 #6
0
def findConsecutiveIntegers(num_integers, num_primes, terminal = None):
    # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    # if terminal == None: terminal = int(1e7)
    if terminal == None: terminal = int(10**(2+num_primes))

    print 'Generating primes from 2-%d' % terminal
    prime_seive = primes_pp.primeSeive(terminal)
    consecutive_integers = []
    print 'Primes successfully generated'

    for cand in xrange(terminal):
        prime_factors = set(primes_pp.getPrimeFactors(cand, prime_seive))
        if len(prime_factors) == num_primes:
            consecutive_integers.append(cand)
        else:
            del consecutive_integers[:]
        if len(consecutive_integers) == num_integers:
            break
    return consecutive_integers
예제 #7
0
def findSpecialOdd(upper_limit = 1000):
    # Generate prime sieve and list of values that are twice a square
    prime_seive = primes_pp.primeSeive(int(2*upper_limit))
    list_of_2_square = [2*x*x for x in xrange(int(math.sqrt(upper_limit)+1))]

    # variables to keep track of whether we found the result
    found_special_odd = False
    special_odd = -1

    # Loop over values 2-upper limit
    for i in xrange(2, upper_limit):
        # trial will be the odd number generated by taking i*2+1
        trial = (2*i) + 1

        # trial will be flagged as "normal" if it can be written as a prime +
        # twice a square
        is_normal = False
        
        # Check list of 2*squares
        for twice_square in list_of_2_square:
            if twice_square > trial:
                break

            # if trial - twice_square = prime, we found our special odd!
            if prime_seive.isPrime(trial - twice_square):
                is_normal = True

        # Stop looking if we found the result
        if not is_normal:
            found_special_odd = True
            special_odd = trial
            break

    # Print result
    if found_special_odd:
        print "Found odd that cannot be written as the sum of prime and twice a square!"
        print '\t%d' % special_odd
    else:
        print "Did not find odd that cannot be written as the sum of prime and twice a square!"

    return special_odd
예제 #8
0
def findLongestSum(terminal = 100):
    print 'Generating primes from 2-%d' % terminal
    prime_seive = primes_pp.primeSeive(terminal)
    print 'Primes successfully generated'
    special_sequences = []

    prime_list = prime_seive.getPrimeList()
    longest_prime = 2
    max_length = 0
    print 'Looping over %d primes' % len(prime_list)
    for i, p in enumerate(prime_list):
        if i%1000 == 0: print 'i: %d -- p: %d' % (i,p)

        this_length = getLongestSum(p, prime_list)
        if this_length > max_length:
            longest_prime = p
            max_length = this_length

    print ''
    print 'prime below %d with longest sum: %d' % (terminal, longest_prime)
    print 'sum length: %d' % max_length
예제 #9
0
def findSpecialSequences(lower_bound = 1000, upper_bound = 3339, delta = 3330):
    max_prime = 10**(int(math.log10(upper_bound)+1))
    print 'Generating primes from 2-%d' % max_prime
    prime_seive = primes_pp.primeSeive(max_prime)
    print 'Primes successfully generated'
    special_sequences = []

    for cand in xrange(lower_bound, upper_bound):
        n1 = cand
        n2 = cand + delta
        n3 = cand + 2*delta
        if not prime_seive.isPrime(n1): continue
        if not prime_seive.isPrime(n2): continue
        if not prime_seive.isPrime(n3): continue

        if not isPermutation(n1, n2): continue
        if not isPermutation(n1, n3): continue

        special_sequences.append([n1, n2, n3])

    print 'Found %d special sequences' % len(special_sequences)
    for ss in special_sequences:
        print '%s -- %d%d%d' % (ss, ss[0], ss[1], ss[2])
    return special_sequences