Problems? Is your data what you think it is? PerlMonks

### Comment on

 Need Help??

I'm not sure quite how you might take advantage of it for optimisation, but the possible decompositions are restricted by the mod 3 equivalences. That is:

```  T_n == 1 (mod 3) when n == 1 (mod 3)
T_n == 0 (mod 3) otherwise
so if we split the T_n sequence into A_n (== 0) and B_n (== 1) the possible decompositions are restricted such that:
```  if n == 0 (mod 3), require A A A or B B B
if n == 1 (mod 3), require A A B
if n == 2 (mod 3), require A B B

You can get similar restrictions by considering other prime moduli, but 3 is likely to be the most beneficial because it has a shorter than possible cycle in T_n.

Also, a word of warning on your p_tri() routine - the final result relies on comparing a floating point number for equality, normally considered a bad idea. It would be preferable to do the test instead by calculating from \$t back up to the last known integer value, something like:

```  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;
}

Hugo

In reply to Re: Triangle Numbers Revisited by hv
in thread Triangle Numbers Revisited by Limbic~Region

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

• Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
• Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
• Read Where should I post X? if you're not absolutely sure you're posting in the right place.
• Posts may use any of the Perl Monks Approved HTML tags:
a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
• You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
 For: Use: & & < < > > [ [ ] ]
• Link using PerlMonks shortcuts! What shortcuts can I use for linking?

Create A New User
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (5)
As of 2018-06-24 08:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
Should cpanminus be part of the standard Perl release?

Results (126 votes). Check out past polls.

Notices?