Pages

Showing posts with label palindrome. Show all posts
Showing posts with label palindrome. Show all posts

Wednesday, July 13, 2011

Problem #4

Problem link
Solution:
package main

import (
"strconv"
)

func isPalindrome(input string) bool {
for i := 0; i < len(input)/2; i++ {
if input[i] != input[len(input)-1-i] {
return false
}
}
return true
}

func main() {
max, mul := 0, 0
for i := 100; i < 1000; i++ {
for j := i; j < 1000; j++ {
mul = i * j
if isPalindrome(strconv.Itoa(mul)) && mul > max {
max = mul
}
}
}
println(max)
}



Result: 906609
Time: 0m0.304s

Problem #36

Problem link
Solution:
package main

import (
"strconv"
)

func main() {
sum := 0
for i := 0; i < 1000000; i++ {
if isPalindrome(strconv.Itoa(i)) {
if isPalindrome(strconv.Itob(i, 2)) {
sum += i
}
}
}
println(sum)
}

func isPalindrome(in string) bool {
for i := 0; i < len(in); i++ {
if in[i] != in[len(in)-i-1] {
return false
}
}
return true
}



Result: 872187
Time: 0m0.801s