=begin output
####
50 = 28 + 1 + 21
51 = 0 + 15 + 36
52 = 21 + 3 + 28
53 = 10 + 28 + 15
54 = 6 + 3 + 45
##
##
=cut
use warnings; use 5.010; use strict;
use POSIX qw"floor";
##
##
sub triang_decompose_1 {
my($goal, $found, $n0, $n1, $n2);
for $goal (0 .. 80) {
$found = 0;
for $n0 (0 .. 12) {
for $n1 (0 .. 12) {
for $n2 (0 .. 12) {
if ($goal == $n0*($n0+1)/2 + $n1*($n1+1)/2 + $n2*($n2+1)/2 && !$found) {
say $goal, " = ", $n0*($n0+1)/2, " + ", $n1*($n1+1)/2, " + ", $n2*($n2+1)/2;
$found = 1;
}
}
}
}
}
}
##
##
sub triang_decompose_2 {
my($goal, $found, $n0, $n1, $n2);
for $goal (0 .. 80) {
$found = 0;
$n0 = 0;
while (!$found && $n0*($n0+1)/2 <= $goal) {
$n1 = 0;
while (!$found && $n1 <= $n0) {
$n2 = 0;
while (!$found && $n2 <= $n1) {
if ($goal == $n0*($n0+1)/2 + $n1*($n1+1)/2 + $n2*($n2+1)/2 && !$found) {
say $goal, " = ", $n0*($n0+1)/2, " + ", $n1*($n1+1)/2, " + ", $n2*($n2+1)/2;
$found = 1;
}
$n2++;
}
$n1++;
}
$n0++;
}
if (!$found) {
say $goal, " has no decomposition.";
}
}
}
##
##
sub triang_decompose_3 {
my($goal, $found, $n0, $n1, $n2, $rem2, $rem1);
for $goal (0 .. 80) {
$found = 0;
$n0 = int(sqrt(2*$goal+1) - 1/2);
while (!$found && 0 <= $n0) {
$rem2 = $goal - $n0*($n0+1)/2;
$n1 = floor(sqrt(2*$rem2+1) - 1/2);
while (!$found && 0 <= $n1) {
$rem1 = $rem2 - $n1*($n1+1)/2;
$n2 = floor(sqrt(2*$rem1+1) - 1/2);
if ($rem1 == $n2*($n2+1)/2) {
say $goal, " = ", $n0*($n0+1)/2, " + ", $n1*($n1+1)/2, " + ", $n2*($n2+1)/2;
$found = 1;
}
$n1--;
}
$n0--;
}
if (!$found) {
say $goal, " has no decomposition.";
}
}
}
##
##
say "Solution 1:";
triang_decompose_1();
say "Solution 2:";
triang_decompose_2();
say "Solution 3:";
triang_decompose_3();
__END__