i suggest using a bit vector for storage (see
vec.) use one bit per number
to store whether it's triangular or not. i'll leave it as an excersize to figure out the details of my implementation.
#!/usr/bin/perl -w
use strict;
$|++;
my $number = $ARGV[0] || die("Usage: trivec.pl number$/");
my $vecTri;
$vecTri = createTriVec( $number );
print $/, findTriCombo( $vecTri, $number );
sub createTriVec
{
my $upper = $_[0] || 99;
my $vec = "\0";
my $guess = 0;
for( my $num = 1; $guess+$num <= $upper; $num++ )
{
$guess+=$num;
vec( $vec, $guess, 1 ) = 1;
}
return $vec;
}
sub findTriCombo
{
my $vec = $_[0] || return undef;
my $num = $_[1] || return undef;
my @tris;
my $sum = 0;
push(@tris, $num, 0, 0), return "@tris" if( vec( $vec, $num, 1 ) )
+;
my $i = $num-1;
while( $i > 0 )
{
# print "<i $i>\t<s $sum>\t<t @tris>$/"; # UNCOMMENT TO SEE WHAT'S GOI
+NG ON...
if( scalar @tris >= 3 ) {
$i = $tris[0] - 1;
@tris = ();
$sum = 0;
}
if( vec( $vec, $i, 1 ) )
{
push @tris, $i;
$sum += $i;
$i = $num - $sum;
next;
}
else
{
$i--;
}
}
if( $sum == $num )
{
push(@tris, 0) while @tris < 3;
return "@tris";
}
return "not found!";
}
Update: changes as per
YuckFoo (below)
Update 2: always returns an array of length three (below)
~Particle ;Þ
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.