From 8c42f6ca1def2713de046c06d57f4e4986617fa9 Mon Sep 17 00:00:00 2001 From: "exercism-solutions-syncer[bot]" <211797793+exercism-solutions-syncer[bot]@users.noreply.github.com> Date: Thu, 22 Jan 2026 19:52:58 +0000 Subject: [PATCH] [Sync Iteration] go/palindrome-products/1 --- .../1/palindrome_products.go | 56 +++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 solutions/go/palindrome-products/1/palindrome_products.go diff --git a/solutions/go/palindrome-products/1/palindrome_products.go b/solutions/go/palindrome-products/1/palindrome_products.go new file mode 100644 index 0000000..e79969c --- /dev/null +++ b/solutions/go/palindrome-products/1/palindrome_products.go @@ -0,0 +1,56 @@ +package palindrome + +import ( + "errors" +) + +// Define Product type here. +type Product struct { + Value int + Factorizations [][2]int +} + +func Products(fmin, fmax int) (Product, Product, error) { + if fmin > fmax { + return Product{}, Product{}, errors.New("fmin > fmax") + } + + var smallest, biggest *Product + for i := fmin; i <= fmax; i++ { + for j := i; j <= fmax; j++ { + prod := i * j + if !isPalindrome(prod) { + continue + } + + if smallest == nil || prod < smallest.Value { + smallest = &Product{Value: prod, Factorizations: [][2]int{{i, j}}} + } else if prod == smallest.Value { + smallest.Factorizations = append(smallest.Factorizations, [2]int{i, j}) + } + + if biggest == nil || prod > biggest.Value { + biggest = &Product{Value: prod, Factorizations: [][2]int{{i, j}}} + } else if prod == biggest.Value { + biggest.Factorizations = append(biggest.Factorizations, [2]int{i, j}) + } + } + } + + if smallest == nil || biggest == nil { + return Product{}, Product{}, errors.New("no palindromes") + } + + return *smallest, *biggest, nil +} + +func isPalindrome(n int) bool { + orig := n + var reversed int + for n != 0 { + reversed = reversed*10 + n%10 + n /= 10 + } + + return orig == reversed +}