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
Solution
let triplets start last =
seq {for i in start..last do
for j in start..last do
yield (float i,float j, sqrt(float((i*i) + (j*j))))}
//functions to get at tuple elemets, instead of matching
let first (x,y,z) = x
let second (x,y,z) = y
let third (x,y,z) = z
let pythTriple1000_2 =
let values = triplets 1 1000 |> Seq.filter (fun x -> first(x) < second(x))
let filter = values |> Seq.filter (fun x -> first(x) + second(x) + third (x) = float 1000)
let result = filter |> Seq.map( fun x -> first(x) * second(x) * third (x))
(Seq.toArray result).[0]
- A Pythagorean triplet is a set of three natural numbers, a b c, for which, a2 + b2 = c2
- For example, 32 + 42 = 9 + 16 = 25 = 52.
- There exists exactly one Pythagorean triplet for which a + b + c = 1000.
- Find the product abc.
Solution
let triplets start last =
seq {for i in start..last do
for j in start..last do
yield (float i,float j, sqrt(float((i*i) + (j*j))))}
//functions to get at tuple elemets, instead of matching
let first (x,y,z) = x
let second (x,y,z) = y
let third (x,y,z) = z
let pythTriple1000_2 =
let values = triplets 1 1000 |> Seq.filter (fun x -> first(x) < second(x))
let filter = values |> Seq.filter (fun x -> first(x) + second(x) + third (x) = float 1000)
let result = filter |> Seq.map( fun x -> first(x) * second(x) * third (x))
(Seq.toArray result).[0]
Comments
Post a Comment