Skip to content

Instantly share code, notes, and snippets.

@thraxil
Created May 1, 2012 15:01
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 thraxil/2568598 to your computer and use it in GitHub Desktop.
Save thraxil/2568598 to your computer and use it in GitHub Desktop.
simple implementation of the mod 7 graph walking algorithm
// simple implementation of the mod 7 algorithm described here:
// http://blog.tanyakhovanova.com/?p=262
// compile with "go build mod7.go"
// then:
//
// $ echo 1098712349087123409871234908712340917823490187234091827340918273490128734 | ./mod7
// 1
//
// time echo 1098712349087123409871234908712340917823490187234091827340918273490128734 | ./mod7
// 1
// echo 109871234908712340987123490871234091782349018723409182734091827349012873 0.00s user 0.00s system 0% cpu 0.001 total
// ./mod7 0.00s user 0.00s system 0% cpu 0.004 total
package main
import (
"fmt"
"strconv"
"strings"
)
func mod7(numStr string) int {
var state int = 0
// the white edges on the graph
white := []int{0, 3, 6, 2, 5, 1, 4}
// black edges on the graph
// (ie, mod 7 as a lookup table for small values)
black := []int{0, 1, 2, 3, 4, 5, 6, 0, 1, 2, 3, 4, 5, 6, 0, 1, 2, 3, 4, 5, 6}
chars := strings.Split(numStr, "")
for c := range chars {
if current, err := strconv.Atoi(chars[c]); err == nil {
state = black[white[state]+current]
} // just skip non-digit input
}
return state
}
func main() {
var num string
fmt.Scanf("%s",&num)
fmt.Println(mod7(num))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment