def sum_primes_below_N(N):
    """ Project euler P    Done !
    Find the sum of all the primes below two million, using the prime module
    """
    import prime  # import from current dir?
    s = 0
    s = sum(prime.primesbelow(N))
    print("sum of all primes below", N, "is : ", s)
def bruteforce():
    """ bruteforce seems not possible
    """
    N = 5 * 10**7
    l = set()
    l2 = [i**2 for i in primesbelow(int(N**0.5))]
    l3 = [i**3 for i in primesbelow(int(N**(1.0 / 3.0)))]
    l4 = [i**4 for i in primesbelow(int(N**0.25))]
    print(l2, l3)
    for i4 in l4:
        for i3 in l3:
            for i2 in l2:
                isum = i2 + i3 + i4
                if isum <= N:
                    l.add(isum)
                else:
                    break
    print(len(l))
Пример #3
0
def problem7():
    """
    By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13.
    What is the 10 001st prime number?
    
    Answer:   104743
    """
    from prime import primesbelow
    print("problem7:", primesbelow(10**6)[10000])
Пример #4
0
def problem5():
    """
    2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder.
    What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?
    
    Answer: 	232792560
    """
    from prime import primesbelow
    print("problem5:", production(primesbelow(20)) * (2**3) * 3)
def bruteforce():
    #
    from prime import primesbelow
    #from math import abs
    ps = set([i for i in primesbelow(10000)
              if i > 1000])  # it must be a 4 digits numbers
    #set is not sorted, even the input is sorted
    print('total primes with exact 4 digits:', len(ps))
    for x in ps:
        #
        s = str(x)
        if len(set(s[:-1])) == 3 and not ('0' in set(s)):
            x1 = int(s[1] + s[2] + s[0] + s[3])
            if x1 in ps:
                x2 = int(s[2] + s[0] + s[1] + s[3])
                if x2 in ps and abs(x1 - x) == abs(x2 - x1):
                    print(sorted([x, x1, x2]))
It's basically the sum of the Euler Totients from phi(2) to phi(1000000). 
"""

from projecteulerhelper import *
#timeit is encluded into projecteulerhelper now


# test the correction by a small dimension first. 
# test  brute force first, method1
#then, try some smart method! 
import math
import prime
from factorization import primefactorize, gcd    # only apply to a number less than 1 million

Denumerator=10**3
primelist=prime.primesbelow(Denumerator)  #math.floor(math.sqrt(Denumerator))  should be enough

def hasCommonFactor(n,d):
    """  return False if no common prime factor,  e.g. (1, any positive number>1)
    return  true if faction n/d can  be reduced any more, 
    this function does not do reduction
    d is prime is tested outside!
    """
    return gcd(n,d)!=1
    """
    print gcd(428523, 999887)  == 142841
    print hasCommonFactor(428523, 999887) 
    #    
    minN=min([n,d]) 
    if minN==1:  return False     #  it must be false,  1 is not a prime, 
    if max([n,d])%minN==0:  return True    #counting for the case d==n
Пример #7
0
Which prime, below one-million, can be written as the sum of the most consecutive primes?
"""


from prime import isprime, primesbelow
import math
#reverse searching jump is better!
# jump length is also relative to l[i],   if floor( N/l[i] ) < M: break
# cal N=100 000 first, to increase the M to search, 

#the error first occur, not searching from smallest prime: 2, then ...

#7 543 997651

N=1000000
l=primesbelow(N//10)  #  N/100 is too small,  
print("primes under", N//10, "=", len(l))
M=21  #  contains 21 terms, and is equal to 953
prime=953
for i in range( len(l)):
    if math.floor( N//l[i] ) < M: 
        print("number higher than ", l[i], "is not worth to search")
        break
    for j in range( M, len(l)-M):  #jump of searching of consecutive primes list, 
        s=sum(l[i:i+j])
        if s>N:
            break
        if s<N and j>M and isprime(s):
            print(l[i],j, s)  
            M=j
        #it takes too long time to finish!