# credmp/Blog

`Also added euler problem 3 solution`
1 parent 5358aa4 commit cbcaf4737b86437d4e227af54921ec0e78c7c95c committed Mar 16, 2013
Showing with 569 additions and 0 deletions.
1. +43 −0 _posts/2013-03-13-Project-Euler-Problem-3.md
43 _posts/2013-03-13-Project-Euler-Problem-3.md
 @@ -0,0 +1,43 @@ +--- +tags: [Erlang, Project Euler] +title: "Project Euler Problem 3" +image: /assets/post_images/Puzzle.JPG +image_credit: Unknown +layout: post +--- + +Problem +------- + +The prime factors of 13195 are 5, 7, 13 and 29. + +What is the largest prime factor of the number 600851475143 ? + +Solution +-------- + +{% highlight erlang %} +-module(problem3). +-export([main/0]). + +solve(N) -> + solve(N, 2, []). + +solve(N, CurDiv,Acc) -> + if + CurDiv > N div 2 -> + N; + true -> + if + N rem CurDiv == 0 -> + io:format("[~p] ", [CurDiv]), + solve(N div CurDiv, CurDiv, Acc ++ [CurDiv]); + true -> + solve(N, CurDiv + 1, Acc) + end + end. + +main() -> + io:format("Problem 3~n"), + solve(600851475143). +{% endhighlight %}