Skip to content

Instantly share code, notes, and snippets.

@ChristianSiegert
Created June 17, 2012 11:24
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ChristianSiegert/2944246 to your computer and use it in GitHub Desktop.
Save ChristianSiegert/2944246 to your computer and use it in GitHub Desktop.
Solution written in Go for challenge 2012-03-23 on ProgrammingPraxis.com
// Exercise from: http://programmingpraxis.com/2012/03/23/base-26-arithmetic/
// Exercise: Write a function that takes two base-26 numbers in which digits are represented by letters with A=0, B=1, … Z=25 and returns their product using the same notation. As an example, CSGHJ × CBA = FNEUZJA.
package main
import (
"fmt"
"math"
)
func main() {
fmt.Println("base26Multiply('CSGHJ', 'CBA'):", base26Multiply("CSGHJ", "CBA"))
}
func base26Multiply(a, b string) string {
return base10ToBase26(base26ToBase10(a) * base26ToBase10(b))
}
func base26ToBase10(a string) (number int) {
for index, value := range a {
number += int(float64(value-65) * math.Pow(26, float64(len(a)-index-1)))
}
return
}
func base10ToBase26(number int) (converted string) {
for number > 0 {
remainder := number % 26
converted = string(remainder+65) + converted
number = (number - remainder) / 26
}
return
}
@ChristianSiegert
Copy link
Author

Console output:

$ go run challenge-2012-03-23.go
base26Multiply('CSGHJ', 'CBA'): FNEUZJA

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment