// Package palindrome deals with palindrome products.
package palindrome
import (
"fmt"
"strconv"
)
// Product represents a product number with all the factors.
type Product struct {
Product int
Factorizations [][2]int
}
// Products finds palindrome products between fmin and fmax.
func Products(fmin, fmax int) (pmin, pmax Product, err error) {
if fmin > fmax {
return Product{}, Product{}, fmt.Errorf("fmin > fmax")
}
for i := fmin; i <= fmax; i++ {
for j := i; j <= fmax; j++ {
if pal(i * j) {
if pmin.Product == 0 {
pmin = Product{i * j, [][2]int{{i, j}}}
}
if i*j == pmax.Product {
pmax.Factorizations = append(pmax.Factorizations, [2]int{i, j})
}
if i*j > pmax.Product {
pmax = Product{i * j, [][2]int{{i, j}}}
}
}
}
}
if pmin.Product == 0 && pmax.Product == 0 {
return Product{}, Product{}, fmt.Errorf("no palindromes...")
}
return pmin, pmax, nil
}
func pal(in int) bool {
s := strconv.Itoa(in)
for i := 0; i < len(s)/2; i++ {
if s[i] != s[len(s)-1-i] {
return false
}
}
return true
}