Created
January 7, 2016 00:14
-
-
Save aabizri/0ff8a04cdb7fd78cbd92 to your computer and use it in GitHub Desktop.
Algorithm for creating self-descripting numbers in several bases
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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