Re: OT: Need help with numeric bases
From: | H. S. Teoh <hsteoh@...> |
Date: | Tuesday, February 25, 2003, 0:56 |
On Mon, Feb 24, 2003 at 05:35:38PM -0600, Peter Clark wrote:
> On Monday 24 February 2003 05:01 pm, H. S. Teoh wrote:
> > Obviously, if the result of the decrement is less than 0, the next digit
> > after it will be decremented ("borrowed" from), as is shown above. If it
> > becomes 0, then the process is repeated: substitute 8 and decrement next
> > digit, etc.. (Oh BTW Peter, I made a mistake in the private reply, where I
> > said a sequence of 0's turns into 8's -- only the first digit becomes 8,
> > the rest becomes 7's.)
> Hmm...this is going to be an interesting mini-challenge to whip up a script
> that will convert between ash[*] and decimal. I would need to keep track of
> whether a zero has already occured or not, as well as some system by which a
> preceeding digit can be decremented to either 8 or 7.
[snip]
#!/usr/bin/perl
sub ash{my$n=$_[0]-1;($n>=0)?ash(int($n/8)).($n%8+1):""}print ash(@ARGV),"\n"
:-)
OK, OK, less evil version below:
#!/usr/bin/perl
sub ash {
my $n = $_[0] - 1;
if ($n >= 0) {
return ash(int($n/8)) . ($n%8 + 1);
}
}
print ash(@ARGV),"\n";
This can, of course, be generalized to any base: just change the 8's to
whatever base you fancy.
T
--
Let's call it an accidental feature. -- Larry Wall
Replies