Project Euler: My #1 occupation this holiday!

Due to the craziness that is my current obsession with Project Euler (I'm solving the puzzles using Haskell, a relatively little-known functional language), I'm going to post my solutions on here as I go (I've got a bit of a backlog currently as I've solved around 15 so far). Note that I try to keep the code as simple as possible (and am proud of any one-liners)!

Problem 1: <html><div style="text-align: left;">
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;">solution1 = sum (filter (x `mod` 5 == 0 && x `mod` 3 == 0) [1..1000])</span></div></html> 


Problem 2:
fib 1 = 1
fib 2 = 1
fib n = fib (n-2) + fib (n-1)
solution2 = sum (filter (\x -> fib x && even x) [1..4000000])
</code>

Problem 3:
<code>
factorsForPrimes n = filter (\x -> n `mod` x == 0) [1..(floor (sqrt (fromIntegral n)))]

isPrime n = if number (factorsForPrimes n) == 1
               then True
            else False
   
smartPrimes n = 2 : filter (isPrime) [3..n]
</code>

More soon but that's all for now!




Comments

Popular posts from this blog

Practicing my rusty Tamil

On creative endeavours, and self-belief, and vision.

Believing without seeing