def problem35(n):
    """
    >>> problem35(1000000)
    55
    """
    primes = [2, 3]
    prime.primesBelow_fast(50, primes)
    prime.primesBelow_fast(100000, primes)
    prime.primesBelow_fast(n, primes)
    primes = [str(p) for p in primes]

    #to make it fast comment out above lines 
    #uncomment below line 

    #primes = prime.get_primes_from_file()
    
    evens = ['0', '2', '4', '6', '8']
    required_primes = filter(lambda s: not set(s).intersection(evens), primes)
    result = 1
        
    for num in required_primes:
        value = lib.rotation(list(num), required_primes)
        result = result + value

    return result
def problem37(n):
    """
    >>> problem37(1000000)
    748317
    """
    primes_below_bilion = [2, 3]
    prime.primesBelow_fast(50, primes_below_bilion)
    prime.primesBelow_fast(100000, primes_below_bilion)
    prime.primesBelow_fast(n, primes_below_bilion)
    
    primes = {}
    for p in primes_below_bilion:
        primes.setdefault(len(str(p)), []).append(str(p))

    #to make it fast comment out above lines 
    #uncomment below line 

    #primes = prime.get_primes_dict_from_file()

    result = [] 
    for L in primes.values():
        call_is_truncatable_prime = lambda n: lib.is_truncatable_prime(n, primes)
        result.extend(filter(call_is_truncatable_prime, L))

    return sum(map(lambda x: int(x), result))
Esempio n. 3
0
def problem50():
    primesList = [2,3,5,7,11,13,17,19,23,29,31,37,41,43,47]
    prime.primesBelow_fast(1000000, primesList)
    primesDict = dict.fromkeys(primesList)
    
    resultDict = {}

    length, count, limit = len(primesList), 1, primesList[-1]
    currentList = primesList[:]
    diffList = [0]*length
    while length:
	result = [x+y-z for x,y,z in zip(currentList[:-1],currentList[1:], diffList) if x+y-z<limit]
	length, count = len(result), count+1
	diffList, currentList = currentList[1:length], result[:]
	if count%2:
	    resultDict[count] = [n for n in result[1:] if primesDict.has_key(n)]
	else:
	    n = result and result[0] or 0
	    resultDict[count] = primesDict.has_key(n) and [n] or []
    
    R = filter(lambda x: resultDict[x], resultDict.keys())
    R.sort()
    return R[-1], resultDict[R[-1]]
def problem27():
    """http://projecteuler.net/index.php?section=problems&id=27
    >>> problem27()
    -59231
    """
    max_pair = (0,0,0)
    primes = [2,3,5,7,11,13,17,19,23,29,31,37,41,43,47]
    for a in xrange(-999, 1000):
        for b in xrange(max(2, 1-a), 1000): # b >= 2, a + b + 1 >= 2
            n, count = 0, 0
            while True:
                v = n*n + a*n + b
                v = v <=0 and -v or v
                if v > primes[-1]:
                    prime.primesBelow_fast(v, primes)
                    if prime.isprime_fast(v, primes): count = count + 1
                    else: break
                else:
                    if not prime.hasFactors(v, primes): count = count + 1
                    else: break
                n = n + 1
            if count > max_pair[2]:
                max_pair = (a,b,count)
    return max_pair[0] * max_pair[1]
Esempio n. 5
0
import prime

primes = [2,3,5,7,11,13,17,19,23,29]
prime.primesBelow_fast(int(1000**0.5+1), primes)

def problem47():
    n, count = 6882, 0#2*3*31*37
    while count != 4:
	primeFactors = prime.primeFactors(n, primes)
	if len(primeFactors)==4:
	    count = count+1
	else:
	    count=0
	n=n+1
    return n-4
print problem47()
Esempio n. 6
0
import prime
primes = [2,3,5,7,11,13,17,19,23,29,31,37,41,43,47]
prime.primesBelow_fast(103, primes)
def problem49():
    choice = xrange(1489, 3340, 2)
    for n in choice:
	if not prime.hasFactors(n, primes):
	    m1, strn = n+3330, str(n)
	    strm1, setn = str(m1), set(strn)
	    if set(strm1)==setn and not prime.hasFactors(m1, primes):
		m2 = m1+3330
		if set(str(m2))==setn and not prime.hasFactors(m2, primes):
		    return strn+strm1+str(m2)    
print problem49()




Esempio n. 7
0
import prime

primes = [2,3,5,7,11,13,17,19,23,29,31,37,41,43,47]
squares = dict.fromkeys((x*x for x in xrange(1, 10000)), 1)
prime.primesBelow_fast(10000, primes)
for x in xrange(35, 10000, 2):
    if not prime.isprime(x):
        is_goldbach = 0
        for p in primes[1:]:
            if p >= x: break
            if squares.has_key((x - p)/2):
                is_goldbach = 1
                break
        if not is_goldbach:
            print x
            break