72 people like it.

Largest Palindrome Number from Product of Two Three Digit Numbers

Here is an improved version twice shorter, than original

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
19: 
20: 
21: 
22: 
23: 
24: 
25: 
//largest palindrone number made from product of two 3-digit numbers

let reverse (s:string) = new string(s |> Seq.toArray |> Array.rev)

let isPalindrone (x,y) =
  let s = string(x*y)
  s = reverse(s) 

let calculateSequence = Seq.unfold (fun (x,y) ->
  if(isPalindrone (x,y)) then
    Some((x,y), (x,(y-1)))
  else if(x=100 && y=100) then
    None
  else if(y=100) then
    Some((0,0), ((x-1),(x-1)))
  else
    Some((0,0), (x,(y-1)))) (999,999)
  

let max = 
  calculateSequence
  |> Seq.filter (fun x -> x>(1,1)) 
  |> Seq.maxBy (fun e -> fst(e)*snd(e))  

printfn "%A" max
val reverse : s:string -> string

Full name: Script.reverse
val s : string
Multiple items
val string : value:'T -> string

Full name: Microsoft.FSharp.Core.Operators.string

--------------------
type string = System.String

Full name: Microsoft.FSharp.Core.string
module Seq

from Microsoft.FSharp.Collections
val toArray : source:seq<'T> -> 'T []

Full name: Microsoft.FSharp.Collections.Seq.toArray
module Array

from Microsoft.FSharp.Collections
val rev : array:'T [] -> 'T []

Full name: Microsoft.FSharp.Collections.Array.rev
val isPalindrone : x:int * y:int -> bool

Full name: Script.isPalindrone
val x : int
val y : int
val calculateSequence : seq<int * int>

Full name: Script.calculateSequence
val unfold : generator:('State -> ('T * 'State) option) -> state:'State -> seq<'T>

Full name: Microsoft.FSharp.Collections.Seq.unfold
union case Option.Some: Value: 'T -> Option<'T>
union case Option.None: Option<'T>
val max : int * int

Full name: Script.max
val filter : predicate:('T -> bool) -> source:seq<'T> -> seq<'T>

Full name: Microsoft.FSharp.Collections.Seq.filter
val x : int * int
val maxBy : projection:('T -> 'U) -> source:seq<'T> -> 'T (requires comparison)

Full name: Microsoft.FSharp.Collections.Seq.maxBy
val e : int * int
val fst : tuple:('T1 * 'T2) -> 'T1

Full name: Microsoft.FSharp.Core.Operators.fst
val snd : tuple:('T1 * 'T2) -> 'T2

Full name: Microsoft.FSharp.Core.Operators.snd
val printfn : format:Printf.TextWriterFormat<'T> -> 'T

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.printfn
Next Version Raw view Test code New version

More information

Link:http://fssnip.net/12
Posted:13 years ago
Author:Nick Canzoneri
Tags: sequence , project euler problem