http://projecteuler.net/index.php?section=problems&id=42
let read_names (file : string) =
は、
let read_names file =
ではなぜか動きません。
open System
open System.IO
let rec split (str : string) p1 p2 mode =
if p2 = str.Length then
[]
else if mode = 0 then
if str.[p2] = '"' then
split str (p2 + 1) (p2 + 1) 1
else
split str p1 (p2 + 1) 0
else
if str.[p2] = '"' then
str.[p1..p2-1] :: (split str p1 (p2 + 1) 0)
else
split str p1 (p2 + 1) 1
let read_names (file : string) =
let s = new StreamReader(file)
let str = s.ReadLine()
s.Close ()
List.sort (split str 0 0 0)
let to_number s =
List.sum [ for c in s -> (int c) - (int 'A') + 1 ]
let is_square n =
let m = int (sqrt (double (n)))
m * m = n
let is_triangle n = is_square (1 + 8 * n)
let a = read_names "words.txt"
printfn "%d" (List.length (List.filter is_triangle
(List.map to_number a)))