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
- By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13.
- What is the 10001st prime number?
My original solution was slow, and the code a bit ugly. The new solution is more idiomatic and dynamic than the original.
let isPrime n =
let upperBound = int32(sqrt(float(n)))
let allNumbers = [2..upperBound]
let divisors = allNumbers |> List.filter (fun div -> n % div = 0 && n <> div)
if List.length divisors = 0 then
true
else
false
let rec recursePrimes num count max =
if count = max then
num-1
else
if isPrime num then
recursePrimes (num+1) (count+1) max
else
recursePrimes (num+1) (count) max
let UT_recursePrimes = recursePrimes 2 0 10001
Solution (Original)
open System
let primeFind find =
let x = (find /10) * (find /10)
let upperBound = Convert.ToInt32(System.Math.Sqrt(Convert.ToDouble(x)))
let allNumbers = ref [2..x] in
for div = 2 to upperBound do
allNumbers := List.filter(fun num -> (num % div = 0 || div >= num)) !allNumbers
!allNumbers
let resPrimes = primeFind(10001).[10000]
Comments
Post a Comment