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.

Haskell agus Tionscadal Euler #3

Seo an chéad phost sa tsraith seo.

Mo réiteach féin do uimhir a trí:

factors n = [m | m <- [1,2..floor(sqrt(fromIntegral n))], n `mod` m == 0]

prime n = null [m | m <- factors n, m > 1, m /= n]

primeFactors n = [m | m <- factors n, prime m]

main = print ( last ( primeFactors 600851475143 ) )

Tá mé réasúnta sásta leis. Ar dtús, bhí an chéad sraith 1..n, ach tar éis cúig bomaite, níor tháinig an freagra! Ansin, stad mé an sraith ag fréamh chearnach n, agus fuair mé an freagra gan mhoill.

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

Haskell agus Tionscadal Euler #2

Seo an chéad phost sa tsraith seo.

Mo réiteach féin do uimhir a do:

fib 1 = 1
fib 2 = 2
fib n = (fib (n-1)) + (fib (n-2))

fiblist = map fib [1..]

shortlist = takeWhile (< 4000000) fiblist
main = print ( sum[n | n <- shortlist, n `mod` 2 == 0] )

Ach nuair a léigh mé an foram agus cúpla suíomh eile, fuair mé cúpla fheabhsú:

fiblist = 0 : 1 : (zipWith (+) fiblist (tail fiblist))
shortlist = takeWhile (< 4000000) fiblist
main = print ( sum[n | n <- shortlist, even n] )

Fuair mé an sainmhíniú Fibonacci is cliste ón alt seo.

If you're wondering what this is all about, here's the first post in this series. The first bit of code is my own solution; the second is a much better one that I found on the forum.

29 August 2009

Haskell agus Tionscadal Euler

Tá mé ag foghlaim Haskell anois. Is teanga suimiúil ríomhaire é, teanga feidhmeána ("feidhmeán" sa chiall matamaitice). Agus tháinig mé ar Tionscadal Euler le déanaí, suíomh idirlín lán de fadhbanna beaga matamaitice. Is maith iad mar chleachtadh chlárú ríomhaire Seo mo réiteach do uimhir a haon:

multipleOf m n = m `mod` n == 0
chosenNumbers = filter (\x -> x `multipleOf` 3 || x `multipleOf` 5) [1..999]
main = print ( sum chosenNumbers )

Ach nuair a léigh mé an fóram, chonaic mé an chaoi is fearr liom:

sum [n | n <- [1..999], n `mod` 5 == 0 || n `mod` 3 == 0]

I'm learning Haskell now. It's a functional programming language (functional in the mathematical sense). And I came across Project Euler recently, which has short maths problems that are great for practising programming. The first bit of code is my own solution; the second is a much better one that I found on the forum.

27 August 2009

Xmonad

Bhain mé triall as Xmonad le déanaí, agus is breá liom é! Is bainisteoir fuinneoga é, scríofa as Haskell. Tá sé iontach mear, agus soshaincheaptha.

Recently I tried Xmonad, and I love it! It's a window manager written in Haskell. It's very fast, and easy to customise.

17 August 2009

Teachtaireacht grinn i nGentoo

Bhí mé ag déanamh "emerge" iontach mór, agus chonaic mé an teachtaireacht seo:

"Unpacking OO.o build tree - [ go and have some tea ] ..."

An-chomhairle ar fad!