Created
May 1, 2012 15:01
-
-
Save thraxil/2568598 to your computer and use it in GitHub Desktop.
simple implementation of the mod 7 graph walking algorithm
This file contains hidden or 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
// 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