05 September 2009

Haskell agus Tionscadal Euler #5

Seo an chéad phost sa tsraith seo.

Mo réiteach féin do uimhir a cúig:

divisibleUpTo n 1 = True
divisibleUpTo n m = ((n `mod` m) == 0) && (n `divisibleUpTo` (m-1))

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]

lowestNumberDivisibleUpTo 1 = 1
lowestNumberDivisibleUpTo n = head [m | m <- [start..], m `divisibleUpTo` n]
where start = if prime n
then n * lowestNumberDivisibleUpTo (n-1)
else lowestNumberDivisibleUpTo (n-1)

main = print ( lowestNumberDivisibleUpTo 20 )

Bhí orm an "start" a úsáid mar bhí an cód ró-mhall gan é.

Fuair mé feabhsú níos giorra ar an fóram:

foldl (lcm) 1 [1..20]


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

No comments: