Skip to content

Instantly share code, notes, and snippets.

@jayrbolton
Last active August 28, 2015 23:13
Show Gist options
  • Save jayrbolton/3b7f3f1200cdb841de91 to your computer and use it in GitHub Desktop.
Save jayrbolton/3b7f3f1200cdb841de91 to your computer and use it in GitHub Desktop.
{- In Haskell, using lazy list comprehension -}
isPalindrome str = even (length str) && (take half str) == (reverse $ drop half str)
where half = length str `div` 2
answer = maximum [x*y | x <- [100..999], y <- [100..999], (isPalindrome $ show (x*y))]
/* In JS, the imperative way */
function isPalindrome(n) {
var str = String(n)
if(str.length % 2 !== 0) return false
var half = str.length / 2
return str.slice(0, half) === str.slice(half).split('').reverse().join('')
}
function answer() {
var max = 0
for(var i = 100; i < 1000; ++i) { for(var j = 100; j < 1000; ++j) {
var prod = i * j
if(isPalindrome(prod) && prod > max) max = prod
}}
return max
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment