Sha256: 18a425ad57fc760564858137a82645b664b37586aaeb17bd5e71a1889ed017cc
Contents?: true
Size: 592 Bytes
Versions: 66
Compression:
Stored size: 592 Bytes
Contents
package luhn func Valid(n string) bool { d := extract(n) if len(d) == 0 { return false } last := len(d) - 1 return (check(d[:last])+d[last])%10 == 0 } func extract(n string) []int { d := make([]int, 0, len(n)) for _, r := range n { if r >= '0' && r <= '9' { d = append(d, int(r-'0')) } } return d } func check(d []int) int { for i := len(d) - 1; i >= 0; i -= 2 { x := 2 * d[i] if x > 9 { x -= 9 } d[i] = x } s := 0 for _, x := range d { s += x } return s } func AddCheck(raw string) string { return raw + string('0'+(10-check(extract(raw))%10)%10) }
Version data entries
66 entries across 66 versions & 1 rubygems