Sunday, July 4, 2010

Problem 43: Find the sum of all 0 to 9 pandigital numbers with the given property.

Combining our Digits and permutations implementations with F#’s built-in Seq functions, we really see how functional programming shines in describing complex algorithms simply.

let problem43a =  
    let hasProperty p = 
        let ddd = p |> Seq.skip 1
                    |> Seq.windowed 3
                    |> Seq.map Digits.toInt 
        
        Seq.zip ddd [2;3;5;7;11;13;17]
        |> Seq.forall (fun (a,b) -> a % b = 0)
            
    permutationsAsc [0;1;2;3;4;5;6;7;8;9]
    |> Seq.filter hasProperty
    |> Seq.sumBy Digits.toInt64

No comments:

Post a Comment