I was exploring Jupyter notebooks , that combines live code, markdown and data, through Microsoft's implementation, known as MS Azure Notebooks , putting together a small library of R and F# notebooks . As Microsoft's FAQ for the service describes it as : ...a multi-lingual REPL on steroids. This is a free service that provides Jupyter notebooks along with supporting packages for R, Python and F# as a service. This means you can just login and get going since no installation/setup is necessary. Typical usage includes schools/instruction, giving webinars, learning languages, sharing ideas, etc. Feel free to clone and comment... In R Azure Workbook for R - Memoisation and Vectorization Charting Correlation Matrices in R In F# Charnownes Constant in FSharp.ipynb Project Euler - Problems 18 and 67 - FSharp using Dynamic Programming
Description
- The decimal number, 585 = 1001001001 (binary), is palindromic in both bases.
- Find the sum of all numbers, less than one million, which are palindromic in base 10 and base 2.
- (Please note that the palindromic number, in either base, may not include leading zeros.)
Solution (Improved)
let isPalindromicBase2 (n:int) =
let forward = System.Convert.ToString(n,2) |> Seq.map (fun c -> int c - int '0') |> Seq.toArray
let rev = Array.rev forward
if forward = rev then
true
else
false
let reverse (s:string) = new string(s |> Seq.toArray |> Array.rev)
let getPalindromesBase10 =
let base10Array = [|1..1000000|] |> Array.filter (fun x -> string x = reverse(string x))
let base02Array = base10Array |> Array.filter (fun x -> isPalindromicBase2 x)
Array.sum base02Array
Solution (Original)
let isPalindromicBase2 (n:int) =
let forward = System.Convert.ToString(n,2) |> Seq.map (fun c -> int c - int '0') |> Seq.toArray
let rev = Array.rev forward
if forward = rev then
true
else
false
let reverse (s:string) = new string(s |> Seq.toArray |> Array.rev)
let getPalindromesBase10 =
let base10Array = [|1..1000000|] |> Array.filter (fun x -> string x = reverse(string x))
let base02Array = base10Array |> Array.filter (fun x -> isPalindromicBase2 x)
Array.sum base02Array
Solution (Original)
let reverseToBinaryList (n:int) =
System.Convert.ToString(n,2)
|> Seq.map (fun c -> int c - int '0')System.Convert.ToString(n,2)
|> Seq.toArray
|> Array.rev
let convertToBinaryList (n:int) =
System.Convert.ToString(n,2)
|> Seq.map (fun c -> int c - int '0')
|> Seq.toArray
let reverse (s:string) = new string(s |> Seq.toArray |> Array.rev)
let getPalindromesBase10 =
let base10Array = [|1..1000000|]
|> Array.filter (fun x -> string x = reverse(string x))
let base02Array = base10Array
|> Array.filter (fun x -> convertToBinaryList x = reverseToBinaryList x)
Array.sum base02Array
Array.sum base02Array
Comments
Post a Comment