Skip to main content

Euler 0015

The Problem:

How many paths can you take if you just walk through a grid by only moving down and right?
We are tryinglooking tofor find20x20. the2x2 longesthas Collatza sequencegiven undervalue 1000000.
of 6.

Considerations:

TheAs collatzit sequenceturns followsout, 2 simple rules:
- if nthis is even,just thenthe n/2max value of all the odd layers of pascal's triangle.  
-1  if n is odd, then n*3 +
1

5 -> 16 -> 8 -> 4 -> 2 -> 1  
61 ->2 51  ->
1 3 3 1  
1 4 ->6 3 -> 2 ->4 1  

The Approach:

TheSo approachif I'llwe generate 41 rows of pascal then we can take tothe thismax value and that is to set up a dictionary of lengths at key n.  If the key doesn't have a value then it generates a collatz sequence until it returns 1 or a key that has a length valueanswer.


The Code:

highest_startingpascal_rows = 100000041

lengths_dictdef generate_pascals_last_row(rows):
    last_row = {1:1}[1]
    max_length#we = 1
length_key = 1

#the recursive function to generate collatz.
#Base case is satisfied by the lengths_dict abovehave already havinggenerated row 1 initialized.
def update_lengths(number, lengths):
    #print(number)
    if lengths.get(number) == None:
        if number % 2 == 0:
            one_down = number/2
        else:
            one_down = number*3 + 1
        lengths = update_lengths(one_down, lengths)
        lengths[number] = lengths[one_down] + 1

    return lengths
    for i in range(1,highest_startingrows-1):
        +#take the first number (hint, it's 1)
        current_row = [1]

        #add up all the numbers and put them in between
        for j in range(0, len(last_row) - 1):
            ifcurrent_row lengths_dict.get(i) =+= None:[last_row[j] lengths_dict+ last_row[j+1]]

        #take the last number (hint, it's 1)
        current_row += [1]
        last_row = update_lengths(i,current_row 
    
    lengths_dict)return if lengths_dict.get(i) > max_length:
        length_key = i
        max_length = lengths_dict.get(i)last_row

print(length_key, max_length)max(generate_pascals_last_row(pascal_rows)))