MCPcopy Create free account
hub / github.com/TheAlgorithms/Go / Luhn

Function Luhn

checksum/luhn.go:13–30  ·  view source on GitHub ↗

Luhn validates the provided data using the Luhn algorithm.

(s []byte)

Source from the content-addressed store, hash-verified

11
12// Luhn validates the provided data using the Luhn algorithm.
13func Luhn(s []byte) bool {
14 n := len(s)
15 number := 0
16 result := 0
17 for i := 0; i < n; i++ {
18 number = int(s[i]) - '0'
19 if i%2 != 0 {
20 result += number
21 continue
22 }
23 number *= 2
24 if number > 9 {
25 number -= 9
26 }
27 result += number
28 }
29 return result%10 == 0
30}

Callers 2

TestLuhnFunction · 0.92

Calls

no outgoing calls

Tested by 2

TestLuhnFunction · 0.74