My Weblog

Blog about programming and math

Solving Project Euler Problems in Haskell

Currently i am trying to learn Haskell which i motivated to learn because of functional approach in python. Initially it was like cryptic but as i started learning , the cryptic part converted into motivation to make code as much short as possible and possibly introducing cryptic part. There is lot a tutorial on Haskell. I followed Learn Haskell for Great good , Real world Haskell and there are many more on Haskell Learning. Apart from that if you want to learn a language then apply that. I tried to solve some initial problems on project Euler. Although i am not a expert of Haskell but i am trying to learn and i am feeling that this language is cool and possible one of the best functional language( This is my second encounter of functional language after python 🙂 ). Here are some Haskell codes which i used to solve project Euler problems.

--problem 1
--sum' ::[Int]->Int
--sum' (p:ps) 	| length (p:ps) ==1   = p 
--		| length (p:ps) >=2   = p + sum' ps
--		| otherwise =0 
plist = [x|x<-[1..999],(mod x 3) ==0 || (mod x 5) ==0]
--s = sum' plist
s= sum plist

--problem 2
sum' :: [Integer]->Integer
sum' (p_1:ps) | p_1>=4000000 &&  (mod p_1 2) ==0   = 0
	      | p_1<4000000 && (mod p_1 2) ==0 = p_1 + sum' ps
	      | otherwise   =  sum' ps
fib :: [Integer]
fib = 1:2:zipWith (+) fib (tail fib)
s_1 :: Integer
s_1 = sum' fib
s_2 :: Integer
s_2 = sum  $ filter (even) $ takeWhile (<4000000) fib

--problem 3
prime ::Integer-> Bool
prime n=primet n plist
primet :: Integer-> [Integer]-> Bool
primet n (p:ps) | n==2 = True
		| (n<=1) || (mod n 2) ==0 = False
		| p*p> n = True
		| ( mod n p )==0 = False
		| otherwise  = primet n ps
plist :: [Integer]
plist = 2:3:filter prime [5,7..]
factor :: Integer->[Integer]
factor n = fact n plist
fact :: Integer->[Integer]->[Integer]
fact n (p:ps) 	| p*p> n = [n] 
	     	| (mod n p) ==0 = p:fact (div n p) (p:ps)
		| otherwise = fact n ps

--problem 4

fun_1 :: Integer->Integer
fun_1 n = rev n (len' n)  where 
		rev n t | n==0 = 0
			| otherwise = (mod n 10)*(10^t) + rev (div n 10) (t-1) 
		
		len' n  | n<10 = 0
			| otherwise = 1 + len' ( div n 10)
fun_2:: Integer-> Bool
fun_2 n = n == fun_1 n

s= maximum $ filter fun_2 [x*y | x<-[100..999],y<-[x..999]]

--problem 5

s :: Integer
s= foldr lcm 1 [1..20]

--problem 6

fun :: Integer->Integer
fun n = (div (n*n*(n+1)*(n+1)) 4) - (div (n*(n+1)*(2*n+1)) 6)
s= fun 100

--problem 7

prime :: Integer-> Bool
prime n = primet n plist
primet :: Integer->[Integer]->Bool
primet n (p:ps) | p*p>n = True  -- chk upto square root 
		| (mod n p)==0 = False
		| otherwise = primet n ps
plist :: [Integer]
plist = 2:filter prime [3,5..]
s::Integer
s=plist!!10000

--problem 9
s :: Integer
s= head [(a*b*c)|m<-[1..],n<-[1..m-1], let a=2*m*n ,let b=(m^2 - n^2) ,let c = (m^2 + n^2), a+b+c == 1000 && (a^2 + b^2 == c^2)]
Advertisements

June 15, 2010 - Posted by | Programming

No comments yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: