Showing posts with label Euler Project. Show all posts
Showing posts with label Euler Project. Show all posts

Wednesday, November 12, 2014

Euler Project: Problem 2 in Haskell

This is my solution in Haskell to the second problem:


I still get a bit dizzy when I think about the second solution...

You will find a great explanation of the second solution in this post:
Fibonacci numbers: the slow way or the fast and lazy way

You'll find the solutions in Haskell to Project Euler problems that I've done so far in this GitHub repository.

Thursday, November 6, 2014

Euler Project: Problem 1 in Haskell

This is my solution in Haskell to the first problem:

I used a list comprehension.

Another way using also a lambda to create a helper:



Bonus: Two ways to do the same in Clojure:

Sunday, August 31, 2014

Euler Project: Problem 5 in Clojure

I solved the fifth problem from Euler project in Clojure.

My first approach was not successful:


Even though it could compute the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder, it didn't work for 1 to 20.
It consumed to much memory.

So I used pen and paper to devise a better approach to solve the problem.

I saw that the numbers from 1 to 10 which multiplied produce 2520 are 9, 8, 7 and 5.
What these numbers have in common is that they all have one prime factor with the highest possible multiplicity in the 1 to 10 range.

This is the code that I derived from those two facts:


which yields the right result.

Update:

As Leo pointed out in his comment, the answer is the least common multiple of the numbers from 1 to 20.

This is the final (more elegant and much simpler than the previous one) solution to the problem:

Euler Project: Problem 4 in Clojure

This is my solution in Clojure to the fourth problem from Euler project:


It was nice to practise with for.

Monday, August 25, 2014

Euler Project: Problem 3 in Clojure

This is my solution in Clojure to the third problem from Euler project:


The largests-prime-factor function can be also written in this alternative way using Clojure's ->> macro:

Tuesday, August 5, 2014

Euler Project: Problem 2 in Clojure

This is my solution in Clojure to the second problem from Euler project:

I created some helpers just to make the solution a bit more readable.