Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
euler 92
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