예제 #1
0
def solve():
    dp = [0] * (N + 1)
    dp[0] = 1

    for i, p in enumerate(list_primes(N)):
        for j in xrange(p, N + 1):
            dp[j] += dp[j - p]

    return [i for i, num_ways in enumerate(dp) if num_ways > 5000][0]
예제 #2
0
def solution():
	ceiling = 100000  # Arbitrary initial cutoff
	primes = common.list_primes(ceiling)

	def find_set_sum(prefix, targetsize, sumlimit):
		if len(prefix) == targetsize:
			return sum(primes[i] for i in prefix)
		else:
			istart = 0 if (len(prefix) == 0) else (prefix[-1] + 1)
			for i in range(istart, len(primes)):
				if primes[i] > sumlimit:
					break
				if all((is_concat_prime(i, j) and is_concat_prime(j, i)) for j in prefix):
					prefix.append(i)
					result = find_set_sum(prefix, targetsize, sumlimit - primes[i])
					prefix.pop()
					if result is not None:
						return result
			return None
	
	
	# Tests whether concat(primes[x], primes[y]) is a prime number, with memoization.
	@common.memoize
	def is_concat_prime(x, y):
		return is_prime(int(str(primes[x]) + str(primes[y])))
	
	
	# Tests whether the given integer is prime. The implementation performs trial division,
	# first using the list of primes named 'primes', then switching to simple incrementation.
	# This requires the last number in 'primes' (if any) to be an odd number.
	def is_prime(x):
		if x < 0:
			raise ValueError()
		elif x in (0, 1):
			return False
		else:
			end = common.sqrt(x)
			for p in primes:
				if p > end:
					break
				if x % p == 0:
					return False
			for i in range(primes[-1] + 2, end + 1, 2):
				if x % i == 0:
					return False
			return True
	
	
	sumlimit = ceiling
	while True:
		setsum = find_set_sum([], 5, sumlimit - 1)
		if setsum is None:  # No smaller sum found
			return str(sumlimit)
		sumlimit = setsum
예제 #3
0
def solve():
    primes = list_primes(50000000)
    result = set()

    for p in primes:
        for q in primes:
            n = p * q
            if n > 100000000:
                break
            else:
                result.add(n)

    return len(result)
예제 #4
0
def solve():
    primes = list_primes(1000000)
    result = {}

    for x in primes[:300:-1]:
        for y in primes[300:]:
            n = x * y
            if n > 10000000:
                break
            phi = totient(n)
            if is_permutation(n, phi):
                result[n] = float(n) / phi

    return min(result.iteritems(), key=operator.itemgetter(1))[0]
예제 #5
0
def do_first_pass():
    mr = MillerRabin()
    concatenates = defaultdict(bool)
    primes = list_primes(LIMIT)
    pairs = set()

    for i, p1 in enumerate(primes):
        for p2 in primes[i + 1:]:
            if mr.is_prime(int(str(p1) + str(p2))) and mr.is_prime(
                    int(str(p2) + str(p1))):
                pairs.add(frozenset([p1, p2]))
                concatenates[(p1, p2)] = True
                concatenates[(p2, p1)] = True

    return pairs, reduce(frozenset.union, pairs), concatenates
예제 #6
0
def solution():
    ceiling = 50000000
    primes = common.list_primes(common.sqrt(ceiling))

    sums = {0}
    for i in range(2, 5):
        newsums = set()
        for p in primes:
            q = p**i
            if q > ceiling:
                break
            for x in sums:
                if x + q <= ceiling:
                    newsums.add(x + q)
        sums = newsums
    return str(len(sums))