Skip to content

Instantly share code, notes, and snippets.

@theburningmonk
Created June 1, 2015 11:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save theburningmonk/d491e98da165c6de180d to your computer and use it in GitHub Desktop.
Save theburningmonk/d491e98da165c6de180d to your computer and use it in GitHub Desktop.
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