#!/usr/bin/env python3
# -*- coding: utf-8 -*-

'''
Problem 138
Consider the isosceles triangle with base length, b = 16, and legs, L = 17.


By using the Pythagorean theorem it can be seen that the height of the triangle, h = √(172 − 82) = 15, which is one less than the base length.
With b = 272 and L = 305, we get h = 273, which is one more than the base length, and this is the second smallest isosceles triangle with the property that h = b ± 1.
Find ∑ L for the twelve smallest isosceles triangles for which h = b ± 1 and b, L are positive integers.
'''
from decimal import Decimal
from math import modf


# Pythagorean approximations
# http://oeis.org/A195615 (FORMULA)
def a(n):
    if n == 0:
        return 15
    if n == 1:
        return 273
    if n == 2:
        return 4895
    return 17 * a(n - 1) + 17 * a(n - 2) - a(n - 3)


def p138():
    highs = [a(i) for i in range(0, 12)]
    result = []
    for h in highs:
        hd = h ** 2
        bd = ((h - 1) // 2) ** 2
        ret = Decimal(hd + bd).sqrt()
        ret_float, ret_int = modf(ret)
        if ret_float == 0.0:
            # print('[-]', [h], ret, ret_float, ret_int)
            result.append(int(ret_int))
            continue
        bd = ((h + 1) // 2) ** 2
        ret = Decimal(hd + bd).sqrt()
        ret_float, ret_int = modf(ret)
        if ret_float == 0.0:
            # print('[+]', [h], ret, ret_float, ret_int)
            result.append(int(ret_int))
    print(sum(result))


p138()

@@ -554,16 +554,44 @@
 rs.%0A'''%0A
+from itertools import count%0A
 from dec
@@ -1492,15 +1492,934 @@
 ult))%0A%0A%0A
+'''%0A@youth4ever solution%0AUsing the Pythagorean Triplets and remarking that if we use :%0Ah = m**2-n**2%0Ab = 2*m*n%0AL = m**2 + n**2%0Awe can start with (m,n) = (4,1) next sequence will be :%0A(m,n)= (17, 4), then (m,n)= (72, 17), (m,n)=(305, 72)... and so on.%0A%0ATherefore the pattern becomes obvious. I will not insist on that. Here is the solution, about 10 ms in Python3 :%0A%0A'''%0Adef special_Isosceles_Tiangles(how_many) :%0A    cnt, S = 0, 0%0A    m, n = 4, 1%0A    while True :%0A        for m in count(m, 1):%0A            h = m ** 2 - n ** 2%0A            b = 2 * m * n%0A            L = m ** 2 + n ** 2%0A            if b * 2 == h + 1 or b * 2== h - 1:%0A                cnt += 1%0A                print('(m,n)=' ,m,n,'        (h, b, L)= ', (h, b ,L) )%0A                S += L%0A                break%0A        n = m%0A        m = int(4.236 * n)%0A        if cnt == how_many :%0A            return print('%5CnAnswer :', S)%0A%0Aspecial_Isosceles_Tiangles(12)%0A# 
 p138()%0A

Add @youth4ever solution with Pythagorean Triplets