Da Vini Code

Simplicity is the ultimate sophistication. – Leonardo Da Vinci.

I am Vini. I like to code. Hence the title.
I love my work. And hence the blog.
I make mistakes. Hence the rotten tomatoes. (!) Er.. Just hope I don’t get that as often.

Combinatorial Algorithms


Combinatorial algorithms(permutations, combinations, powersets, subsets et, al) had never been my forte.

Infact, I was quite scared of them.
Presumably an irrational fear that stuck with after a failed effort in my graduate years.

No matter, This weekend, I set out to remedy that.
I didn’t want to read on the algorithms before implementing and decided I’d go pencil-and-paper and implement one that occurs naturally to me.

Here goes the result of that –


Project Euler Problem 22


Whilst trying Project Euler problems, I often write a prototype in Python before trying out an implementation in Haskell.
This problem however was screaming for “Functional Programming”.

In fact this time around, I started with a Haskell implementation and went on to translate to Python just for kicks. 🙂

Suffice to say, I am very pleased with myself. 😀

import Char
import List

names = [<List of names from problem specification>]

{- For each string s in list, Calculate sum of the characters’ alpha positions -}
sumChars :: [String] -> [Int]
sumChars [] = []
sumChars (x:xs) =
(foldl (\acc ltr -> acc + (ord(ltr)-ord(‘A’) + 1)) 0 x) : sumChars xs

main = do
let sortedNames = sort(names)
let zippedSumAndIndex = zip [1 .. length(sortedNames)] (sumChars sortedNames)
let total = foldl (\x y -> x + fst(y) * snd(y)) 0 zippedSumAndIndex
print total

More Haskell…


So today, I was pondering How difficult it’d be to implement the Sieve of Eratosthenes in Haskell.

Turns out, very easy after all.. and more elegant.

sieve [] = []
sieve (x:xs) = x : sieve [p | p <- xs, p `mod` x /= 0]

So With that figured out, A new challenge was to solve this for any n. O’course, my earlier much simpler solution would have sufficed –

foldl (lcm) 1 [1..20]

But that doesn’t quite use my own implementation of the Sieve of Eratosthenes algorithm, does it?

So that needed to be corrected 😉

So here’s my solution –


For the love of Haskell…


Haskell never fails to impress even the mediocre programmer in me.
Here’s something I have attempted solving using Haskell.

Problem –
Find the sum of all the even-valued terms in the Fibonacci sequence which do not exceed four million.
(Source: Project Euler problem 2)

Go to Top