Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: Triangle Numbers Revisited

by hv (Parson)
on Oct 14, 2004 at 12:47 UTC ( #399183=note: print w/replies, xml ) Need Help??


in reply to Triangle Numbers Revisited

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

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://399183]
help
Chatterbox?
[Lady_Aleena]: What?! Can't stat ~/Music: No such file or directory? The ~/ doesn't expand?
[Lady_Aleena]: And the env variable $HOME doesn't work either. !@#$
[shmem]: no, that's a shell feature. Use $ENV{HOME}
[marioroy]: my $ans = qx{ find \$HOME -name "*.pl" -exec ls -l {} \\; };
[marioroy]: must backslash $ inside qx
[Lady_Aleena]: I haven't written perl for a while, I've been trying to do things at the command line. I'm having to relearn a lot.

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (11)
As of 2017-04-23 21:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I'm a fool:











    Results (432 votes). Check out past polls.