Beispiel #1
0
def main():
    print utils.get_max_vowels(first_sentence)
    print
    print utils.get_max_len(first_sentence)
    print
    print utils.reverse(second_sentence)
    print
    print utils.get_info_for_obj(os)
    print
    print utils.get_info_for_obj(sys)
    print
    print utils.get_pseudo_sum(124)
    print
    print utils.get_primes(10000)
Beispiel #2
0
def main():
    print utils.get_max_vowels(first_sentence)
    print 
    print utils.get_max_len(first_sentence)
    print 
    print utils.reverse(second_sentence)
    print 
    print utils.get_info_for_obj(os)
    print 
    print utils.get_info_for_obj(sys)
    print 
    print utils.get_pseudo_sum(124)
    print 
    print utils.get_primes(10000)
def main():
	primes = utils.get_primes(10**6)
	maxLength = 0
	print len([x for x in primes if x])
	for i in xrange(0,10**6+1):
		for size in xrange(0,i):
			
	return maxLength
def run():
    n = 600851475143
    max_factor = int(math.sqrt(n))
    primes = utils.get_primes(max_factor)
    primes.reverse()
    for p in primes:
        if n % p == 0:
            print p
            break
def fc(N, radius):
    """Generate composites with 2 prime factors less than N, with primes within given radius of sqrt(N)"""
    s = N**.5
    pmin, pmax = (s - radius, s + radius)
    p = filter(lambda x: pmin < x, get_primes(int(pmax)))
    # partition into two groups to avoid looking at both i*j and j*i
    l = [i for i in p if i < s]
    h = [i for i in p if s < i]
    return ((i * j, [i, j]) for i in l for j in h if (i * j) <= N)
def fc(N, radius):
    """Generate composites with 2 prime factors less than N, with primes within given radius of sqrt(N)"""
    s = N**.5
    pmin, pmax = (s - radius, s + radius)
    p = filter(lambda x : pmin < x, get_primes(int(pmax)))
    # partition into two groups to avoid looking at both i*j and j*i
    l = [i for i in p if i < s]
    h = [i for i in p if s < i]
    return ((i*j, [i,j]) for i in l for j in h if (i*j) <= N)
def fcomposites(n, N=10**7):
    """ Returns composites that are the product of 2 distinct prime factors, less than N, and their factors
        This takes about a minute for N=10**7.
    """
    # make a sieve that lists the prime factors of each number in it.
    primes = get_primes(N)
    sieve = [[] for i in xrange(N)]
    for i in xrange(2, N):
        if len(sieve[i]) == 0:
            # prime. update sieve
            for k in xrange(2 * i, N, i):
                sieve[k].append(i)
    return ((i, sieve[i]) for i in xrange(2, N) if len(sieve[i]) == n)
def original_solution():
    """ original_solution took 731.178 ms
        The answer (original) is: 1097343
    """
    N = 50*10**6
    squares = [p**2 for p in get_primes(int(N**(1.0/2.0)) + 1)]
    cubes   = [p**3 for p in get_primes(int(N**(1.0/3.0)) + 1)]
    fourths = [p**4 for p in get_primes(int(N**(1.0/4.0)) + 1)]
    
    # Some numbers can be expressed as the sum of prime powers in two different ways.
    # I'm looking at you, 145 = 121 + 8 + 16 = 4 + 125 + 16.
    numbers = set()
    S, C = 0, 0 
    for f in fourths:
        b = N - f
        C = bisect(cubes, b)
        for c in cubes[:C]:
            S = bisect(squares, b - c)
            for s in squares[:S]:
                numbers.add(f + c + s)
    
    return len(numbers)
def fcomposites(n, N=10**7):
    """ Returns composites that are the product of 2 distinct prime factors, less than N, and their factors
        This takes about a minute for N=10**7.
    """
    # make a sieve that lists the prime factors of each number in it.
    primes = get_primes(N)
    sieve = [[] for i in xrange(N)]
    for i in xrange(2, N):
        if len(sieve[i]) == 0:
            # prime. update sieve
            for k in xrange(2*i, N, i):
                sieve[k].append(i)
    return ((i, sieve[i]) for i in xrange(2, N) if len(sieve[i]) == n)
Beispiel #10
0
def original_solution():
    """ original_solution took 731.178 ms
        The answer (original) is: 1097343
    """
    N = 50 * 10**6
    squares = [p**2 for p in get_primes(int(N**(1.0 / 2.0)) + 1)]
    cubes = [p**3 for p in get_primes(int(N**(1.0 / 3.0)) + 1)]
    fourths = [p**4 for p in get_primes(int(N**(1.0 / 4.0)) + 1)]

    # Some numbers can be expressed as the sum of prime powers in two different ways.
    # I'm looking at you, 145 = 121 + 8 + 16 = 4 + 125 + 16.
    numbers = set()
    S, C = 0, 0
    for f in fourths:
        b = N - f
        C = bisect(cubes, b)
        for c in cubes[:C]:
            S = bisect(squares, b - c)
            for s in squares[:S]:
                numbers.add(f + c + s)

    return len(numbers)
Beispiel #11
0
def original_solution():
    """Some insight here: we're basically looking for the largest product of unique primes less than N
       How many primes do we need to look at?
       1M = 10^6 = (2*5)^6, so we need at most 12 primes, so we can stop at 19.
       
       runtime on mbp is 0.040ms (< 1ms!!!)
    """
    N = 1000 * 1000
    
    product = 1
    for p in get_primes(19):
        product *= p
        if N < product:
            return product / p
    return None
    13, 23, 43, 53, 73, and 83, are all prime.

By replacing the 3rd and 4th digits of 56**3 with the same digit, this 5-digit number 
is the first example having seven primes among the ten generated numbers, yielding the family: 
56003, 56113, 56333, 56443, 56663, 56773, and 56993. 

Consequently 56003, being the first member of this family, is the smallest prime with this property.

Find the smallest prime which, by replacing part of the number (not necessarily adjacent digits) 
with the same digit, is part of an eight prime value family.
"""

from timed import timed, loop_timer
from utils import get_primes

primes = get_primes()
digits = map(str, range(10))

def prime_family_size(p):
    sp = str(p)
    m = 0
    for c in set(str(p/10)): # ignore lowest digit, since only 1,3,7,9 are valid values
        if sp.count(c) < 2: continue
        l = 0
        for d in digits:
            q = sp.replace(c, d)
            if int(q) in primes and q[0] != '0': # leading 0 doesn't count (e.g. 03)
                # print q
                l += 1
        if m < l:
            m = l
Beispiel #13
0
02 January 2004

The primes 3, 7, 109, and 673, are quite remarkable. 
By taking any two primes and concatenating them in any order the result will always be prime. 
For example, taking 7 and 109, both 7109 and 1097 are prime. 
The sum of these four primes, 792, represents the lowest sum for a set of four primes with this property.

Find the lowest sum for a set of five primes for which any two primes concatenate to produce another prime.
"""
from math import log10
from timed import timed
import utils
from collections import defaultdict

LIMIT = 9000  # just a guess
all_primes = utils.get_primes(LIMIT)
# minor optimization
all_primes.remove(2)
all_primes.remove(5)


def is_prime(n):
    if n < LIMIT:
        return n in all_primes

    return utils.is_prime(n)


def test(i, j):
    # f = lambda x, y : int(str(x) + str(y)) # get a 1-second speedup by doing this with math
    order = lambda x: 10**(1 + int(log10(x)))
import utils

def prime_sums(sieve):
	sums = [0]*len(sieve)
	for i in xrange(0,len(sieve)):
		if sieve[i]:
			sums[i] += i
		else:
			sums[i] = -1
	print sums






# generate enough primes
sieve = utils.get_primes(10*6)
sums = prime_sums(sieve)
# to store the sum and length in format:
# [sum] = length of sum (number of primes that adds to sum)	
results = dict()

primes = [x for x in xrange(0,len(sieve)) if sieve[x]]

for i in xrange(0, len(sums)):
	if sieve[sums[i]]:
		results[sums[i]] = primes.index(i) # get the position of i in primes
	else:
prime_sums(sieve)
Beispiel #15
0
def getp(n):
    return sorted(get_primes(n), reverse=True)
02 January 2004

The primes 3, 7, 109, and 673, are quite remarkable. 
By taking any two primes and concatenating them in any order the result will always be prime. 
For example, taking 7 and 109, both 7109 and 1097 are prime. 
The sum of these four primes, 792, represents the lowest sum for a set of four primes with this property.

Find the lowest sum for a set of five primes for which any two primes concatenate to produce another prime.
"""
from math import log10
from timed import timed
import utils
from collections import defaultdict

LIMIT = 9000 # just a guess
all_primes = utils.get_primes(LIMIT)
# minor optimization
all_primes.remove(2)
all_primes.remove(5)

def is_prime(n):
    if n < LIMIT: 
        return n in all_primes

    return utils.is_prime(n)


def test(i, j):
    # f = lambda x, y : int(str(x) + str(y)) # get a 1-second speedup by doing this with math
    order = lambda x : 10**(1 + int(log10(x)))
    f = lambda x, y : x * order(y) + y
import utils
import time

# brute force is way too slow
# consider digit sum:
#
# 1+2+3+4+5+6+7+8+9 = 45
# 1+2+3+4+5+6+7+8 = 36
# 1+2+3+4+5+6+7 = 28
# 1+2+3+4+5+6 = 21
# 1+2+3+4+5 = 15
# 1+2+3+4 = 10
# 1+2+3 = 6
# 1+2 = 3
#
# everything but 4 and 7 is divisible by 3 so we
# only need to check those two. 

s = time.time()
all_primes = utils.get_primes(7654321)
answer = []

for i in xrange(0,len(all_primes)):
	if (len(str(i)) == 4 or len(str(i))==7):
		if all_primes[i]:
			if utils.isPandigital(i):
				answer.append(i)

print answer[-1]
print time.time() - s
Beispiel #18
0
from utils import get_primes

if __name__ == "__main__":
    L = 10 ** 8
    primes = get_primes(L / 2)
    ans = 0
    for i in xrange(0, len(primes)):
        if primes[i] >= 10000:
            break
        for j in xrange(i, len(primes)):
            if primes[i] * primes[j] > L:
                break
            ans += 1
    print primes[-1]
    print ans