Problem 140

completed January 22, 2021

Comments

This problem was very very similar to problem 137. I simply followed the same steps I used to solve that one to solve this one as well. For this one though the square root of the determinant ended up being a bit stranger. Whereas in the last problem it was a straight forward F(4*n + 1) where F is the Fibonacci sequence. Here, the value of the determinant was also reliant on whether this was an odd or even valued golden nugget.

Code

def gfib(n):
    f1, f2 = 1, 4
    for _ in range(n - 2):
        f1, f2 = f2, f1 + f2
    return f2

def fib(n):
    f1, f2 = 1, 1
    for _ in range(n - 2):
        f1, f2 = f2, f1 + f2
    return f2

def detfor(i):
    n = gfib(2*i + 1)
    if i % 2:
        n += fib(2*i + 1)
    return n

def solve(n):
    total = 0
    for i in range(1, n + 1):
        det = detfor(i)
        Ax = (-14 + (176 + 20*det**2)**0.5) // 10
        total += Ax
    return int(total)

if __name__ == '__main__':
    import sys
    n = eval(sys.argv[1])
    print(solve(n))

Tests

import pytest
from problem import solve

_test_solve = (
        (20, 375079985),
)

@pytest.mark.parametrize('n,expect', _test_solve)
def test_solve(n, expect):
    assert expect == solve(n)