Problem 4
module Problem_0004 (* 【問題】 左右どちらから読んでも同じ値になる数を回文数という。 2桁の数の積で表される回文数のうち、最大のものは 9009 = 91 × 99 である。 では、3桁の数の積で表される回文数のうち 最大のものはいくらになるか。 *) そもそも間違い //60ms~62ms //698896 let calc = let inc = Seq.init 900 (fun x -> x + 100); Seq.zip inc inc |> Seq.map (fun (x,y) -> x * y) |> Seq.filter (fun num -> let oring = num.ToString().ToCharArray() let rev = oring |> Array.rev oring = rev) let run () = calc |> Seq.max
修正版
//575ms~582ms //906609 let calc2 = seq { for x in 100..999 do for y in 100..999 -> x*y } |> Seq.filter (fun num -> let oring = num.ToString().ToCharArray() let rev = oring |> Array.rev oring = rev) let run () = calc2 |> Seq.max
■コンピュテーション式
module Problem_0004_Monado (* 【問題】 左右どちらから読んでも同じ値になる数を回文数という。 2桁の数の積で表される回文数のうち、最大のものは 9009 = 91 × 99 である。 では、3桁の数の積で表される回文数のうち 最大のものはいくらになるか。 *) type PalindromeBuilder() = let (^^) x n = [ for a in 1..n -> x ] |> Seq.reduce(*) member this.Bind(v,f)= let s = seq { (10^^(v-1))..((10^^v)-1) } f <| Seq.zip s s member this.For(s,f) = s |> Seq.map(fun (a,b) -> a*b) |> Seq.filter(fun n -> let a = n.ToString().ToCharArray() let b = a |> Array.rev a = b ) |> Seq.map(fun n -> f n) member this.Yield v = v member this.Run v = v |> Seq.max let pal = PalindromeBuilder() let calc() = pal { let! a = 3 for x in a -> x } let run () = calc()
-
-
- -
-