31 August 2009

Haskell agus Tionscadal Euler #4

Seo an chéad phost sa tsraith seo.

Mo chéad réiteach do uimhir a ceathair:

reverseNum n = (read s)::Integer
where s = reverse( show n )

isPalindrome n = n == reverseNum n

factors :: (Integral b, Integral a) => a -> [(a, b)]
factors n = [(i,j) | i <- [1,2..floor(sqrt(fromIntegral n))], let j=floor((fromIntegral n)/(fromIntegral i)), n `mod` i == 0 ]

threeDigitFactors n = [(i,j) | (i,j) <- factors n, i > 99, i < 1000, j > 99, j < 1000]

isProductOfThreeDigitFactors n = not (null (threeDigitFactors n))

candidates = [n | n <- [100*100..999*999], isPalindrome n, isProductOfThreeDigitFactors n]

main = print ( head (reverse candidates) )

Tar éis leamh an fóráim, scríobh mé an ceann seo:

reverseNum n = (read s)::Integer
where s = reverse( show n )

isPalindrome n = n == reverseNum n

candidates = [n*m | n <- [999,998..100], m <- [999,998..n], isPalindrome (n*m)]

main = print ( maximum candidates )

Shíl mé nách mbeidh sé tapa go leor mar bhain mé úsáid as "maximum", ach bhí sé chomh tapa leis an céad cheann.

If you're wondering what this is all about, here's the first post in this series.

1 comment:

Anonymous said...

floor((fromIntegral n)/(fromIntegral i))

should be written as

n `div` i