Пример #1
0
def find_shortest_part(matrix):
    
    N = len(matrix)
    # info will store the info data structure (dictionary) for each cell
    info = [[ { 'path': None, 'd': 10 ** 9 } for x in range(N)] for x in range(N)]

    info[0][0]['d'] = matrix[0][0]
    info[0][0]['path'] = []

    not_visited = []

    for y in range(len(matrix)):
        for x in range(len(matrix[y])):
            not_visited.append( P2D(x, y) )

    while True:

        rest = sorted(not_visited, key=lambda p: info[p.y][p.x]['d'])

        project_euler.report_progress( 1 - len(rest) / N ** 2 )

        if len(rest) == 0: break # all is visited

        current = rest[0]

        neighbors = list(get_neighbors(matrix, current))

        for neighbor in neighbors:
            dist = info[current.y][current.x]['d'] + matrix[neighbor.y][neighbor.x]

            if info[neighbor.y][neighbor.x]['d'] > dist: # found shorter way
                
                info[neighbor.y][neighbor.x]['d'] = dist
                info[neighbor.y][neighbor.x]['path'] = list(info[current.y][current.x]['path']) + [ P2D(current.x, current.y) ]

        not_visited.remove( current )

    min_d = info[-1][-1]['d']
    min_path = info[-1][-1]['path'] + [ P2D(N-1, N-1) ]

    print ("MIN distance: {0}, PATH: {1}".format(min_d, min_path))
Пример #2
0
import project_euler
import math

max = 50 * 10 ** 6

primes = project_euler.eratosphen_primes( math.sqrt( max ) )

values = set()

done = 0
for p_sq in primes:
    
    done += 1
    project_euler.report_progress( done / len(primes) )

    for p_cube in primes:

        if p_sq ** 2 + p_cube ** 3 > max: break

        for p_dsq in primes:
            
            v = p_sq ** 2 + p_cube ** 3 + p_dsq ** 4

            if v < max: values.add( v )
            else: break

print( len(values) )
Пример #3
0
def checkOnRightTriangle(P, Q):
    a = P[0] ** 2 + P[1] ** 2
    b = Q[0] ** 2 + Q[1] ** 2
    c = (P[0] - Q[0]) ** 2 + (P[1] - Q[1]) ** 2

    (a, b, c) = sorted([a, b, c])

    return c == a + b

n = 50
r = 0

for x1 in range(n + 1):

    project_euler.report_progress( x1 / n )

    for y1 in range(n + 1):
        for x2 in range(n + 1):
            for y2 in range(n + 1):

                if x1 == 0 and y1 == 0 or x2 == 0 and y2 == 0:
                    continue

                if x1 == x2 and y1 == y2:
                    continue

                if checkOnRightTriangle( (x1, y1), (x2, y2) ):
                    r += 1

print( r // 2 )