Skip to content

Instantly share code, notes, and snippets.

@aabizri
Created January 7, 2016 00:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aabizri/0ff8a04cdb7fd78cbd92 to your computer and use it in GitHub Desktop.
Save aabizri/0ff8a04cdb7fd78cbd92 to your computer and use it in GitHub Desktop.
Algorithm for creating self-descripting numbers in several bases
// by @nodvos alexandre@bizri.fr
package main
import (
"fmt"
"strconv"
)
// Use it small endian (index 0 is the unit)
type number []uint
var it uint
func (s number) serial() string {
var n string
for _,k := range(s) {
n += strconv.FormatInt(int64(k),len(s))
}
return n
}
func (s number) has(digit uint) uint {
var n uint
for _,k := range(s) {
if k == digit {
n += 1
}
}
return n
}
func (s number) correct() bool {
it+=1
if it > 100 {
return true
}
return false
}
func main() {
for i := 4;i<=39;i++ {
it = 0
final := make(number,i)
for !final.correct() {
for j,_ := range(final) {
final[j]=final.has(uint(j))
}
}
fmt.Printf("Base %v: %s \n",i,final.serial())
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment