Turning number ranges into prefixes

Mark Blackman mark at blackmans.org
Thu Aug 24 09:21:15 BST 2006


On 23 Aug 2006, at 14:34, David Cantrell wrote:

> I have a little problem that I could do with some help with.  I have
> data specifying number ranges.  For example, 1424210000 -  
> 1424225999.  I
> need to turn that into a list of prefixes like 142421, 1424220,  
> 1424221,
> 1424222, 1424223, 1424224, 1424225.  That range includes all the  
> numbers
> of the right length with those prefixes, and no others.  Is there a
> terribly clever way of generating the list?
>
> If it makes things easier, the length is actually irrelevant -  
> these are
> phone numbers, so 14242221 is impossible, it has to be 1424222XXX.

Obviously, very simple to do the following, but doesn't find the
*minimum* number of (non-degenerate) prefixes,  but still much
less than the maximum number  for this case and quick. Finding all the
non-degenerate prefixes efficiently for the general case is a bit
involved at first glance, although I get the feeling it might not take
a lot to extend this a bit further.

my ( $min, $max );

$min = $ARGV[0] or $min = 1;
$max = $ARGV[1] or $max = $min;

while ($min % 10 == 0
     && $max % 10 == 9 )
{

     $min = int( $min / 10 );
     $max = int( $max / 10 );
}


my @prefixes=$min..$max;

print join("\n", at prefixes),"\n";


>
> -- 
> David Cantrell | Hero of the Information Age



More information about the london.pm mailing list