Your skill will accomplishwhat the force of many cannot PerlMonks

Re (tilly) 1: Power of two round up.

by tilly (Archbishop)
 on Dec 15, 2000 at 23:15 UTC ( #46894=note: print w/replies, xml ) Need Help??

in reply to Power of two round up.

TIMTOWTDI
```sub binround {
my \$out = 1;
my \$in = shift;
while (\$in) {
\$in >>= 1;
\$out += \$out;
}
\$out;
}

Replies are listed 'Best First'.
Re: Re (tilly) 1: Power of two round up.
by japhy (Canon) on Dec 15, 2000 at 23:31 UTC
And an even faster WTDI:
```sub next_pwr {
my (\$x,\$p) = (@_,2);  # default to next_pwr(X,2)
my \$log = log(\$x)/log(\$p);
\$log = int(\$log+1) if \$log != int(\$log);
return \$p**\$log;
}
You can even add a log() memoization in there:
```{
my %LOG = (2 => log(2));  # most common
sub next_pwr {
my (\$x,\$p) = (@_,2);  # default to next_pwr(X,2)

# assertions, etc.
die "illegal base: \$p" if \$p < 2;
return 1 if \$x == 1;
\$p = int \$p;

my \$log =
(\$LOG{\$x} ||= log(\$x)) / (\$LOG{\$p} ||= log(\$p));
\$log = int(\$log+1) if \$log != int(\$log);

return \$p ** \$log;
}
}

japhy -- Perl and Regex Hacker

Create A New User
Node Status?
node history
Node Type: note [id://46894]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (6)
As of 2017-08-22 17:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
Who is your favorite scientist and why?

Results (337 votes). Check out past polls.

Notices?