sub p_tri { my $num = shift; my $x = 8 * $num + 1; my $t = int((sqrt($x) + 1)/2); return +((2 * $t - 1) * (2 * $t - 1) == $x) ? 0 : --$t; }