module _17 open System.Text.RegularExpressions let rec numberToWords i = let firstDigit number = int (number.ToString().Substring(0,1)) let remainingDigits number = int (number.ToString().Substring(1)) let numberHeadTuple i = (firstDigit i, remainingDigits i) let concatWith separator s1 s2 = if not (System.String.IsNullOrEmpty(s2)) then s1 + separator + s2 else s1 let hyphen = concatWith "-" let concatAnd = concatWith " and " match i.ToString().Length with | 1 -> match i with | 1 -> "one" | 2 -> "two" | 3 -> "three" | 4-> "four" | 5 -> "five" | 6 -> "six" | 7 -> "seven" | 8 -> "eight" | 9 -> "nine" | _ -> "" | 2 -> match numberHeadTuple i with | (1,0) -> "ten" | (1,1) -> "eleven" | (1,2) -> "twelve" | (1,3) -> "thirteen" | (1,4) -> "fourteen" | (1,5) -> "fifteen" | (1,8) -> "eighteen" | (1,j) -> (numberToWords j) + "teen" | (2,j) -> (numberToWords j) |> hyphen "twenty" | (3,j) -> (numberToWords j) |> hyphen "thirty" | (4,j) -> (numberToWords j) |> hyphen "forty" | (5,j) -> (numberToWords j) |> hyphen "fifty" | (6,j) -> (numberToWords j) |> hyphen "sixty" | (7,j) -> (numberToWords j) |> hyphen "seventy" | (8,j) -> (numberToWords j) |> hyphen "eighty" | (9,j) -> (numberToWords j) |> hyphen "ninety" | (_, _) -> "wtf" | 3 -> let (first,remaining) = numberHeadTuple i (numberToWords remaining) |> concatAnd ((numberToWords first) + " hundred") | 4 -> let (first,remaining) = numberHeadTuple i (numberToWords remaining) |> concatAnd ((numberToWords first) + " thousand") | _ -> "unknown" let getNumberWords = let nums = [1..1000] let numWords = Seq.map numberToWords nums //printfn "%s" (String.concat "\n" numWords) let strip s = Regex.Replace(s, "[^a-z]", "") numWords |> Seq.fold (fun acc elem -> acc + (strip elem).Length) 0 //let getNumberWords = // let nums = [1..100] // let strip s = Regex.Replace(s, "[^a-z]", "") // nums // |> Seq.map (fun n -> (n, ((numberToWords >> strip) n).Length, numberToWords n)) // |> Seq.toArray // |> Array.filter (fun (n, l, _) -> n % l = 0)