Example #1
0
def find_solution():
    start_time = datetime.now()
    answer = 0
    primes = []
    progress = ProgressBar(1000000)

    for n in xrange(2, progress.max):
        progress.set_value(n)

        if is_prime(n, primes):
            primes.append(n)

            #~ digits = map(lambda x: int(x), str(n))
            try:
                permutate(map(lambda x: x, str(n)), [], raise_if_not_prime)
                print "Found palindrome: %s" % n
                answer += 1

            except IsNotPrimeError:
                pass

            #~ all_prime = True
            #~ for item in permutate(map(lambda x: x, str(n)), ""):
                #~ if not is_prime(int(item)):
                    #~ all_prime = False
                    #~ break

            #~ if all_prime:

    end_time = datetime.now()
    progress.finished()
    print "answer: %s" % answer
    print "number of primes: %s" % len(primes)
    print "first 10 primes: %s" % primes[:10]
    print end_time - start_time
Example #2
0
def find_solution():
    start_time = datetime.now()
    answer = 0
    primes = []
    progress = ProgressBar(1000000)

    for n in xrange(2, progress.max):
        progress.set_value(n)

        if is_prime(n, primes):
            primes.append(n)

            all_prime = True
            for item in rotate(n):
                if not is_prime(int(item)):
                    all_prime = False
                    break

            if all_prime:
                print "Found palindrome: %s" % n
                answer += 1


    end_time = datetime.now()
    progress.finished()
    print "answer: %s" % answer
    print "number of primes: %s" % len(primes)
    print "first 10 primes: %s" % primes[:10]
    print end_time - start_time
Example #3
0
Find the sum of all the primes below two million.
"""
if __name__ == "__main__":
    import sys, os
    sys.path.append( os.path.join( os.getcwd(), '..' ) )

from datetime import datetime

from euler_tools.prime import is_prime
from euler_tools.progress_bar import ProgressBar

start_time = datetime.now()
sum = 0
primes = []
progress = ProgressBar(2000000)

for n in xrange(2, progress.max):
    progress.set_value(n)

    if is_prime(n, primes):
        sum += n
        primes.append(n)

end_time = datetime.now()
progress.finished()
print "sum: %s" % sum
print "number of primes: %s" % len(primes)
print end_time - start_time

#2,000,000: 142913828922
Example #4
0
Triangle 	  	T(n)=n(n+1)/2       1, 3, 6, 10, 15, ...
Pentagonal      P(n)=n(3n-1)/2     1, 5, 12, 22, 35, ...
Hexagonal 	  	H(n)=n(2n-1)       1, 6, 15, 28, 45, ...

It can be verified that T_(285) = P_(165) = H_(143) = 40755.

Find the next triangle number that is also pentagonal and hexagonal.
"""

if __name__ == "__main__":
    import sys, os
    sys.path.append( os.path.join( os.getcwd(), '..' ) )

from euler_tools.progress_bar import ProgressBar

progress = ProgressBar(1000000)
max_iter = progress.max

for n_t in range(max_iter):
    progress.set_value(n_t)

    #Triangle
    result_t = n_t * (n_t + 1) / 2

    # Pentagonal
    success = False
    for n_p in range(max_iter):
        result_p = n_p * (3 * n_p - 1) / 2

        if result_p == result_t:
            success = True