Created
June 1, 2015 11:40
-
-
Save theburningmonk/d491e98da165c6de180d to your computer and use it in GitHub Desktop.
euler 92
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
open System.Collections.Generic | |
/// Applies memoization to the supplied function f | |
let memoize (f : 'a -> 'b) = | |
let cache = new Dictionary<'a, 'b>() | |
let memoizedFunc (input : 'a) = | |
// check if there is a cached result for this input | |
match cache.TryGetValue(input) with | |
| true, x -> x | |
| false, _ -> | |
// evaluate and add result to cache | |
let result = f input | |
cache.Add(input, result) | |
result | |
// return the memoized version of f | |
memoizedFunc | |
let squareDigit = | |
let inner (c : char) = pown (int(c.ToString())) 2 | |
memoize inner | |
let sosd = | |
let inner (n : int) = n.ToString().ToCharArray() |> Array.sumBy squareDigit | |
memoize inner | |
let rec f n = | |
match sosd n with | |
| 89 -> true | |
| 1 -> false | |
| n -> f n | |
let t = [1..9999999] |> List.filter f |> List.length |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment