in reply to Unbelievably slow..
........................ +....................... .+...................... ++...................... ..+..................... +.+..................... .++..................... +++..................... ...+.................... +..+.................... .+.+.................... ++.+.................... ..++.................... +.++.................... .+++.................... ++++.................... ....+................... +...+................... .+..+................... ++..+................... ..+.+...................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):
Ironically, I mostly found out how to slow the code down rather than speeding it up.#!/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;
Funny enough, it's slow as CDROM.#!/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');
|
---|