If you do a print $num & $shift ? '+' : '.' ; in the loop (following diotalevi's example), you can observer for $num (0..20) the pattern:
........................
+.......................
.+......................
++......................
..+.....................
+.+.....................
.++.....................
+++.....................
...+....................
+..+....................
.+.+....................
++.+....................
..++....................
+.++....................
.+++....................
++++....................
....+...................
+...+...................
.+..+...................
++..+...................
..+.+...................
And the middle part (8388608..8388628):
.......................+
+......................+
.+.....................+
++.....................+
..+....................+
+.+....................+
.++....................+
+++....................+
...+...................+
+..+...................+
.+.+...................+
++.+...................+
..++...................+
+.++...................+
.+++...................+
++++...................+
....+..................+
+...+..................+
.+..+..................+
++..+..................+
..+.+..................+
And the end part (16777196..16777216):
..++.+++++++++++++++++++
+.++.+++++++++++++++++++
.+++.+++++++++++++++++++
++++.+++++++++++++++++++
....++++++++++++++++++++
+...++++++++++++++++++++
.+..++++++++++++++++++++
++..++++++++++++++++++++
..+.++++++++++++++++++++
+.+.++++++++++++++++++++
.++.++++++++++++++++++++
+++.++++++++++++++++++++
...+++++++++++++++++++++
+..+++++++++++++++++++++
.+.+++++++++++++++++++++
++.+++++++++++++++++++++
..++++++++++++++++++++++
+.++++++++++++++++++++++
.+++++++++++++++++++++++
++++++++++++++++++++++++
........................
Looks like a sparse matrix we could explore. Like, if you add if ($num<$shift){$total-=(24-$i)*(25+$i)/2; last;} in the loop, it would speed up the beginning of the loop (but slow down the end). Here a sample code (mostly diotalevi's):
#!/usr/bin/perl -w use strict; use integer; my $ans = 0; for my $num (0..16777216) { my $total = 0; my $shift = 1; for my $i (1..24) { $total += $num & $shift ? $i : -$i; if ($num<$shift){$total-=(24-$i)*(25+$i)/2; last;} $shift <<= 1; } $ans++ if $total == 0; } print $ans;
Ironically, I mostly found out how to slow the code down rather than speeding it up.

Though the $shift <<= 1; part is essentially static and equivalent to @shift = qw/ 1 2 4 8 16 32 64 128 256 512 1024 2048 4096 8192 16384 32768 65536 131072 262144 524288 1048576 2097152 4194304 8388608 /;, using array will slow things down.

If you want something even slower, here is something I tried in Pari:
#!/usr/bin/perl -w use Math::Pari qw/ PARI / ; PARI 'ans=0' ; my $cmd = qq/ for( num = 0, 1677, total=0; shft=1; for( i = 1, 24, if(bitand(num,shft)==0, total+=-i, total+=i); shft <<= 1; ); if(total==0, ans++); ) /; $cmd =~ s/\s//g; PARI $cmd ; print PARI('ans');
Funny enough, it's slow as CDROM.

In reply to Re: Believably slow.. by chunlou
in thread Unbelievably slow.. by kiat

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.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, 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, summary, 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.