[Golang] Largest Palindrome Product - Problem 4 - Project Euler


Go solution to largest palindrome product - Problem 4 - Project Euler. [2]

Problem:

A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 × 99.

Find the largest palindrome made from the product of two 3-digit numbers.

Solution:

906609 = 913 x 993

Run Code on Go Playground

package main

import (
      "fmt"
      "strconv"
)

func IsDecimalPalindromeNumber(n int64) bool {
      if n < 0 {
              n = -n
      }

      s := strconv.FormatInt(n, 10)
      bound := (len(s) / 2) + 1
      for i := 0; i < bound; i++ {
              if s[i] != s[len(s)-1-i] {
                      return false
              }
      }
      return true
}

func main() {
      currentMax := 0
      maxI := 0
      maxJ := 0
      for i := 999; i >= 100; i-- {
              for j := 999; j >= 100; j-- {
                      product := i * j
                      if currentMax > product {
                              break
                      }
                      if IsDecimalPalindromeNumber(int64(product)) {
                              currentMax = product
                              maxI = i
                              maxJ = j
                              continue
                      }
              }
      }
      fmt.Println("answer: ", currentMax)
      fmt.Println("i: ", maxI)
      fmt.Println("j: ", maxJ)
}

References:

[1][Golang] Check if Integer is Palindromic Number
[2]
[3]
[4]
[5]
[6]Rosetta Code
[7]