"There are two cases to consider: triangles with side lengths $(k, k, k-1)$ (perimeter $3k-1$) and triangles with side lengths $(k, k, k+1)$ (perimeter $3k+1$). If we apply [Heron's formula](https://en.wikipedia.org/wiki/Heron%27s_formula), we can compute the areas of these triangles just from the side lengths. For the first case,\n",
"In both cases, we are looking for values of $k$ such that $A$ is an integer - such triangles are called [almost-equilateral Heronian triangles](https://en.wikipedia.org/wiki/Heronian_triangle).\n",
"\n",
"Consider the first case - for $A$ to be an integer, $s(s-k+1)$ must be a square number. With a little bit of algebra, this can be formulated as a [Diophantine problem](https://en.wikipedia.org/wiki/Diophantine_equation) in terms of $k$.\n",
"$$4z^2 = 3k^2 + 2k - 1$$\n",
"Note that we don't really care about the value of $z$ here (beyond being integral) - we just care about what values of $k$ work."
"(See [problem 45](https://projecteuler.net/problem=45) for discussion on solving Diophantine equations like this.)\n",
"\n",
"We get several parametric formulas for $k$, but we only care about side lengths that are positive (duh) and that generate triangles with perimeters below 1000000000. ($k=1$ is also a solution to the Diophantine equation - we exclude it since a 1-1-0 triangle is obviously [degenerate](https://en.wikipedia.org/wiki/Degeneracy_(mathematics)).)"
"evals = {expr(t=i).simplify_full() for expr in sols for i in range(-5, 5)}\n",
"sides = {k for k in evals if k > 1 and 3*k - 1 <= 1000000000}\n",
"sides"
]
},
{
"cell_type": "markdown",
"id": "68e9b708",
"metadata": {},
"source": [
"With our side lengths found, we can easily compute the perimeters."
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "e2c638b6",
"metadata": {},
"outputs": [],
"source": [
"perimeters = {3*k - 1 for k in sides}"
]
},
{
"cell_type": "markdown",
"id": "02177352",
"metadata": {},
"source": [
"For the second case, the Diophantine problem is very similar - just one sign change. (As above, $k=1$ is excluded since a 1-1-2 triangle is degenerate.)"
"* The union of the two cases: [A120893](https://oeis.org/A120893)\n",
"\n",
"#### Copyright (C) 2025 filifa\n",
"\n",
"This work is licensed under the [Creative Commons Attribution-ShareAlike 4.0 International license](https://creativecommons.org/licenses/by-sa/4.0/) and the [BSD Zero Clause license](https://spdx.org/licenses/0BSD.html)."