## Base-26 Arithmetic

### March 23, 2012

We write functions to convert between Prashant strings and Scheme numbers, then use normal Scheme multiplication; the digits and undigits functions of the Standard Prelude make the conversions easy:

`(define (number->prashant n)`

(define (d->c d) (integer->char (+ d 65)))

(list->string (map d->c (digits n 26))))

`(define (prashant->number p)`

(define (c->d c) (- (char->integer c) 65))

(undigits (map c->d (string->list p)) 26))

Here are two examples:

`> (number->prashant 1234567)`

"CSGHJ"

> (prashant->number "CSGHJ")

1234567

Then the multiplication function is simple:

`(define (prashant-times x y)`

(number->prashant

(* (prashant->number x)

(prashant->number y))))

And here’s another example:

> (prashant-times "CSGHJ" "CBA")

"FNEUZJA"

You can run the program at http://programmingpraxis.codepad.org/ZXOpICfm.

Pages: 1 2

PHP CLI:

Maybe it’s too early for me and I made a mistake, but I think that “CSGHJ” x “CBA” = “FOFVAJA”; here’s my Python:

Changing bases seems to work fine, i.e.

`int_to_b26(b26_to_int(s)) == s`

as far as I can tell.Woops, never mind. I tested against 25, instead of 26; let me work on this (make sure I don’t have any more embarassing mistakes) and get back to you later.

Yup. Changing all 25s to 26s does the trick. I won’t repost, since I’ve already filled this page with my comments.

Here is a Perl solution

Ok, I should stop, but this is good J practice for me.

Typically, I wouldn’t then bother with defining the multiply function and instead just use:

Of course, I could always define it if I wanted to:

The trick here, besides having base-conversion built-in (“#.”), is the “under” adverb, “&.”. “f &. g” translates to

J is pretty clever about deducing the inverses of functions, and you can always explicitly set the inverse to a function, if it can’t figure it out.

Hey! Here’s the C code :

Hey! there’s a flaw :P replace the last 3 lines with:

an erlang example.

“FNEUZJA” = base26:mult(“CSGHJ”, “CBA”).

In Haskell, but written for conciseness at the expense of speed and safety. :-)

Looks long now that I’ve seen the other comments, but here’s one in Common Lisp:

Another CL version, in a slightly different style from that of Jonathan.

>>> WholeSym(WholeSym(‘CSGHJ’) * WholeSym(‘CBA’))

WholeSym(‘FNEUZJA’)

>>> W = WholeSym

>>> W( W(‘C’) * W(‘BL’) * W(‘CJ’) * W(‘FV’) * W(‘VHJ’) * W(‘MVUTMJAWRZ’) )

WholeSym(‘PROGRAMMINGPRAXIS’)

Also python version

My solution written in Go lang: https://gist.github.com/2944246

@Siyuan The python one is a bit weird … int_to_b26(25) == ‘z’ int_to_b26(26) == ‘ba’ < should be 'aa'