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

Tuesday, March 23, 2010

Project Euler Problem 2

In my last post I talked about how I've been using the Project Euler problems as a sort of Code Kata.  Thanks to @rickasaurus and @calvinb for providing great feedback/comments on that solution!

Problem 2 is below:

Problem 2: "Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be: 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...  Find the sum of all the even-valued terms in the sequence which do not exceed four million." (Project Euler Problem #2)

Tests:

module Problem2Tests

open NUnit.Framework   
  
[<TestFixture>]    
type Problem2__When_finding_sum_of_even_valued_terms_in_the_fibonacci_sequence_up_to_89 () =   
    [<Test>]  
    member this.should_return_sum_of_44 () =  
        let result = Problem2.FindSum 89  
        Assert.AreEqual(44, result)        

[<TestFixture>]    
type Problem2__When_finding_sum_of_even_valued_terms_in_the_fibonacci_sequence_below_4_million () =   
    [<Test>]  
    member this.should_return_sum_of_4613732 () =  
        let result = Problem2.FindSum 4000000  
        Assert.AreEqual(4613732, result)
Code:

module Problem2

let FindSum max =
    let rec getFibonacciSumForEvens lastValue currentValue accumulator =
        match lastValue + currentValue with
        | sum when sum >= max -> accumulator
        | sum when sum % 2 = 0 -> 
            getFibonacciSumForEvens currentValue sum (accumulator + sum)
        | sum -> getFibonacciSumForEvens currentValue sum accumulator
    getFibonacciSumForEvens 0 1 0 


Saturday, March 20, 2010

Code Kata and Project Euler

Recently, I've been thinking about the concept of Code Kata and the personal improvement that this type of practice can bring.  I started thinking of types of exercises that I could do to utilize this concept.  After looking around on the web, I landed on projecteuler.net and found that the problems listed seemed to fit my agenda perfectly. 

For those of you who don't know, "Project Euler is a series of challenging mathematical/computer programming problems..." (ProjectEuler.net).  The mathematical nature of these problems make them a great fit for the functional paradigm.

Here's the first problem:

"Add all the natural numbers below one thousand that are multiples of 3 or 5."  Additional details include the following simple example: "If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23."

With the help of the simple example, I created a test that looked like this:

 
module Problem1Tests

open NUnit.Framework 

[<TestFixture>]  
type FindPrimes2__When_Getting_Sum_Of_Multiples_Of_3_And_5_to_a_max_number_of_10 () = 
    [<Test>]
    member this.should_return_sum_of_23 () =
        let result = Problem1.GetSumOfMultiplesOf3And5(10)
        Assert.AreEqual(23, result)

After a few refactoring sessions, my final code looked like this:

 
module Problem1

let GetSumOfMultiplesOf3And5 max =
    seq{3..max-1} |> Seq.fold(fun acc number -> 
                        (if (number % 3 = 0 || number % 5 = 0) then 
                            acc + number else acc)) 0 

Have you found good Code Kata exercises?  If so, I'd love to see them.  Also, let me know if you have a better solution for the problem above.