forked from TheAlgorithms/Go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
luhn.go
28 lines (26 loc) · 759 Bytes
/
luhn.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
// lunh.go
// description: Luhn algorithm
// details: is a simple checksum formula used to validate a variety of identification numbers, such as credit card numbers, IMEI numbers, etc [Lunh](https://en.wikipedia.org/wiki/Luhn_algorithm)
// author(s) [red_byte](https://github.com/i-redbyte)
// see lunh_test.go
// Package checksum describes algorithms for finding various checksums
package checksum
// LuhnAlgorithm This function calculates the checksum using the Luna algorithm
func LuhnAlgorithm(s []rune) bool {
n := len(s)
number := 0
result := 0
for i := 0; i < n; i++ {
number = int(s[i]) - '0'
if i%2 != 0 {
result += number
continue
}
number *= 2
if number > 9 {
number -= 9
}
result += number
}
return result%10 == 0
}