There is an analytic solution.
For the (n,n,n+1) triangle, the area is Sqrt[(n-1)(3n+1)]/2
.
Area -> FullSimplify[triangleA[n, n, n + 1]]
The valid values of n are Sloane's A103974, the smaller sides in (n,n,n+1)-integer triangle with integer area.
Table[Simplify[((2 + Sqrt[3])^(2 k) + (2 - Sqrt[3])^(2 k) + 1)/3], {k,0,10}]
{1, 5, 65, 901, 12545, 174725, 2433601, 33895685, 472105985,
6575588101, 91586127425}
For the (n,n,n-1) triangle, the area is Sqrt[(n+1)(3n-1)]/2
.
Area -> FullSimplify[triangleA[n, n, n - 1]]
The valid values of n are Sloane's A103772, the larger sides in (n,n,n-1)-integer triangle with integer area.
Table[
Round[(-1 + (7-4*Sqrt[3])^k*(2+Sqrt[3]) - (-2+Sqrt[3])*(7+4*Sqrt[3])^k)/3],
{k,2,10}]
{17, 241, 3361, 46817, 652081, 9082321, 126500417, 1761923521,
24540428881}
For all valid values of n less than or equal to a maximum m, sum 3n+1
or 3n-1
.
perimeterSum[m_] :=
Block[{area = 0, k, n},
k = 1;
While[(n = Simplify[((2 + Sqrt[3])^(2 k) + (2 - Sqrt[3])^(2 k) + 1)/3]) <= m,
k += 1;
area += (3 n + 1)];
k = 2;
While[(n = Round[(-1 + (7-4*Sqrt[3])^k*(2+Sqrt[3]) - (-2+Sqrt[3])*(7+4*Sqrt[3])^k)/3]) <= m,
k += 1;
area += (3 n - 1)];
area
]
The solution is very fast.
AbsoluteTiming[perimeterSum[10^6]]
{0.001005, 2672274}
If[IntegerQ[...] == True, ...]
. JustIf[IntegerQ[...], ...]
will suffice. In addition,===
(SameQ
) is usually the appropriate check for equality inIf
statements, not==
(Equals
). $\endgroup$