Euler 004 go Solution

Largest palindrome product

Problem

https://projecteuler.net/problem=4

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.

Answer: 906609

Solution

solution004.go

package go_euler

func Solution004() int {
  max_pali := 0
  for i := 999; i > 100; i-- {
    for j := 999; j > 100; j-- {
      t := i * j
      if palindrome_test(t) {
        if t > max_pali {
          max_pali = t
        }
      }
    }
  }
  return max_pali
}

func palindrome_test(test_me int) bool {
  reversed := 0
  original := test_me

  for 0 < original {
    reversed = reversed*10 + (original % 10)
    original /= 10
  }

  return (test_me == reversed)
}

See Also

# cpp go java php ruby rust javascript
1
2
3  
4    
5    
6      
7          
8          
9          
10          
11          
12          
# cpp ruby
13
14
15
16
17
18
19
20  
21  
22  
23  
24