Well, hdb is pretty close, except there's not really a need to guess or search through the array. The smallest step in the compared values is
$ perl -we 'print log(78894000000/63115200000)'
0.22314355131421

Using a suitably small log base, one is assured that no more than one value step can occur between log()..log()+1. Here, int(4.5*log(...)) will do nicely.

#! /usr/bin/perl -l use List::Util qw(first min max); use Data::Dumper; my @G = ( [ 25000, 2500 ], [ 50000, 5000 ], [ 150000, 12500 ], [ 225000, 25000 ], [ 300000, 37500 ], [ 600000, 60000 ], [ 1200000, 120000 ], [ 3600000, 300000 ], [ 5400000, 600000 ], [ 10800000, 900000 ], [ 21600000, 1800000 ], [ 43200000, 3600000 ], [ 64800000, 7200000 ], [ 129600000, 10800000 ], [ 216000000, 21600000 ], [ 432000000, 43200000 ], [ 864000000, 86400000 ], [ 1728000000, 172800000 ], [ 3024000000, 345600000 ], [ 6048000000, 604800000 ], [ 12096000000, 1209600000 ], [ 31557600000, 2629800000 ], [ 63115200000, 5259600000 ], [ 78894000000, 7889400000 ], [ 157788000000, 15778800000 ], ); # construct the lookup table. my @TT = map { my $i=$_; first { $i <= tti($_->[0]) } @G } 0..tti(~0); push @TT, [~0, 31557600000]; print Dumper \@TT; sub tti { -45 + int (4.5 * log(min(max(25000,shift),157788000000))) } sub v2r { my ($v, $i) = ($_[0], tti($_[0])); $TT[$i + ($v >= $TT[$i][0])][1]; } for (map { $_-1, $_, int 1.2*$_ } map $_->[0], @G) { print "v2r($_) == @{[v2r($_)]}"; }

Split array and combined array variants ought be benched as well. In practice, computing the log might be more cumbersome than a small search.


In reply to Re: A better way of lookup? by Anonymous Monk
in thread A better way of lookup? by BrowserUk

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.