Last active
August 28, 2015 23:13
-
-
Save jayrbolton/3b7f3f1200cdb841de91 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{- 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