[comp.sys.mac.programmer] base 10 --> base 3 ...

tedj@hpcilzb.HP.COM (Ted Johnson) (08/23/88)

This isn't really a Mac-specific question, but it *is* for a program
that I'm writing for Macintoshes...


Does anyone know of an algorithm to convert a positive integer N into
its base 3 representation?  (e.g., 14 in base 10 is 112 in base 3).

Any textbook/journal pointers are appreciated!  (Source code is
appreciated even more :-)

-Ted

tedj@hpcilzb.HP.COM (Ted Johnson) (08/23/88)

Never mind!  I just found a solution.  It turned out to be much easier
than I thought...  :-}

-Ted

oster@dewey.soe.berkeley.edu (David Phillip Oster) (08/24/88)

In article <730059@hpcilzb.HP.COM> tedj@hpcilzb.HP.COM (Ted Johnson) writes:
>Does anyone know of an algorithm to convert a positive integer N into
>its base 3 representation? 

You are going to feel pretty silly when I tell you the answer to this:
---------------------------------

ConvertBase(n, base){
	if(n >= base)
		ConvertBase(n/base, base)
	Output(n%base);
}

---------------------------------
where OutPut(digit) will emit the digit as the final result. Note: all the
declarations here default to short int, so I don't have to explicitly
declare anything.

To convert this routine to pascal, declare everything and replace '%' by 
' mod '

so, for example, calling ConvertBase(100, 10) will call:
Output(1)
Output(0)
Output(0)

A sample definition of Output() is:
Output(digit){
	putchar('0' + digit);
}
Note: this routine uses stack space proportional to the log to the base
"base" of its argument "n". Don't call it with a base of 0 or 1! For a
base of 2 or greater, it will never use more than 16 stackframes for 16
bit integers, which is perfectly reasonable.

--- David Phillip Oster            --When you asked me to live in sin with you
Arpa: oster@dewey.soe.berkeley.edu --I didn't know you meant sloth.
Uucp: {uwvax,decvax,ihnp4}!ucbvax!oster%dewey.soe.berkeley.edu

suitti@haddock.ima.isc.com (Steve Uitti) (08/24/88)

In article <730059@hpcilzb.HP.COM> tedj@hpcilzb.HP.COM (Ted Johnson) writes:
>Does anyone know of an algorithm to convert a positive integer N into
>its base 3 representation?  (e.g., 14 in base 10 is 112 in base 3).

There are perhaps an infinite number of ways to do this.  Here's one.
The digits are produced in reverse order, so recursion is used to put
them on the stack for First In Last Out (reverse) ordering.  The "%"
(remainder) operator in C is seldom used, as is indexing into a string
literal.  Note that the input "num" does not really have a radix.
Most machines represent integers using binary, but if integers were
Binary Coded Decimal, this would still work.

/* print num in radix, lead zero suppressed */
void
pnum(num, radix)
int num, radix;
{
	int a;

	a = num % radix;		/* remainder */
	num /= radix;
	if (num != 0)
		pnum(num, radix);	/* call self to finish */
	printf("%c", "0123456789abcdef"[a]);
}

main() { pnum(14, 3); printf("\n"); }	/* test - produces 112 */

	Stephen.

calhoun@m.cs.uiuc.edu (08/25/88)

>---------------------------------
>
>ConvertBase(n, base){
>	if(n >= base)
>		ConvertBase(n/base, base)
>	Output(n%base);
>}
>
>---------------------------------
>where OutPut(digit) will emit the digit as the final result. Note: all the
>declarations here default to short int, so I don't have to explicitly
>declare anything.
>
>To convert this routine to pascal, declare everything and replace '%' by 
>' mod '

I may be mistaken (its been awhile since my pascal days) but I think you also need to change
the / (indicating division) to DIV when converting to pascal.

Jeff Calhoun	University of Illinois Computer Science Dept
                Rm 200/220 Digital Computing Lab
                1304 W. Springfield, Urbana, IL 61801

calhoun@a.cs.uiuc.edu
uiucdcs!calhoun
calhoun@uiucdcs.bitnet

fmodwyer@cs.tcd.ie (Frank O'Dwyer , ext. 1695) (08/26/88)

In article <730059@hpcilzb.HP.COM>, tedj@hpcilzb.HP.COM (Ted Johnson) writes:
> Does anyone know of an algorithm to convert a positive integer N into
> its base 3 representation?  (e.g., 14 in base 10 is 112 in base 3).

The following procedure does this (written in Turbo pascal).

 procedure Base3to10(DecimalNumber:integer; var Base3String : str255);
 begin
  Base3String := '';
  if DecimalNumber = 0
   then Base3String := '0'
  else
   while  DecimalNumber <> 0 do 
    begin
     Base3String := concat(chr((DecimalNumber mod 3) + ord('0')),Base3String);
     DecimalNumber := DecimalNumber div 3;
    end;
 end;

To change to another base, replace occurrences of the number 3 with
the new base.

Hope this helps.