From a66508b48671a70b50c5467800a9c662c193b765 Mon Sep 17 00:00:00 2001 From: "exercism-solutions-syncer[bot]" <211797793+exercism-solutions-syncer[bot]@users.noreply.github.com> Date: Sun, 1 Feb 2026 13:39:58 +0000 Subject: [PATCH] [Sync Iteration] go/luhn/2 --- solutions/go/luhn/2/luhn.go | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 solutions/go/luhn/2/luhn.go diff --git a/solutions/go/luhn/2/luhn.go b/solutions/go/luhn/2/luhn.go new file mode 100644 index 0000000..c2ac9f8 --- /dev/null +++ b/solutions/go/luhn/2/luhn.go @@ -0,0 +1,30 @@ +package luhn + +func Valid(id string) bool { + var sum int + double := false + for i := len(id) - 1; i >= 0; i-- { + r := id[i] + + if r == ' ' { + continue + } + + if r < '0' || r > '9' { + return false + } + + n := int(r - '0') + if double { + n *= 2 + if n > 9 { + n -= 9 + } + } + double = !double + + sum += n + } + + return sum%10 == 0 && (sum > 0 || len(id) > 2) +}