Skip to main content

Posts

Microsoft Azure Notebooks - Live code - F#, R, and Python

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

Project Euler - Problem #15

Description Starting in the top left corner of a 2×2 grid, there are 6 routes (without backtracking) to the bottom right corner. How many routes are there through a 20×20 grid? Solution Thanks to the following for providing the insight required to solve this, based on combinatronics: http://realultimateprogramming.blogspot.com/2009/03/project-euler-problem-15.html let rec factorialBI (n:bigint) (acc:bigint) =     if n <= 1I then         acc     else         let newAcc = n * acc         factorialBI (n-1I) newAcc let combine (sides:bigint) =           let factorial1 = factorialBI (sides * 2I) 1I     let factorial2 = factorialBI sides 1I     factorial1/(factorial2 * factorial2) let UT_combine_2 = combine 2I   let UT_combine_20 = combine 20I

Project Euler Statistics (Popularity and Effectiveness)

Project Euler (PE) provides some statistics, but I wanted to see the effectiveness of the languages in solving problems.  Although PE lists Mathematica as number 1, and on balance of popularity and effectiveness it might be, it is not the language used to solve the highest proportion of problems.  For that, the language and programmers of the following are most effective. Frink (43%) PARI/GP (28%) Magma (21%) MUMPS (18%) Mathematica (17%) For comparison, the results of a few other oher languages Haskell (11%) Python (10%) Perl (10%) Ruby (9%) F# (9%) Scala (8%) C/C++ (8%) Java (8%) C# (8%) The entire list (Zipped Excel files):   ProjectEulerStats.zip

Project Euler - Problem 21

Description Let d(n) be defined as the sum of proper divisors of n (numbers less than n which divide evenly into n). If d(a) = b and d(b) = a, where a ≠ b, then a and b are an amicable pair and each of a and b are called amicable numbers. For example, the proper divisors of 220 are 1, 2, 4, 5, 10, 11, 20, 22, 44, 55 and 110; therefore d(220) = 284. The proper divisors of 284 are 1, 2, 4, 71 and 142; so d(284) = 220. Evaluate the sum of all the amicable numbers under 10000. Solution (Improved) let DivisorsSum num = [1..num] |> List.filter (fun x -> num % x = 0 && x < num) |> List.sum let rec matchLists num max arr =     if num = max then         (List.sum arr)/2     else         let newTest = DivisorsSum num         let check = DivisorsSum(newTest)         if num = check && newTest <> check then   ...

Project Euler - Problem #14

Description The following iterative sequence is defined for the set of positive integers:  - n → n/2 (n is even)  - n → 3n + 1 (n is odd) Using the rule above and starting with 13, we generate the following sequence: 13 → 40 → 20 → 10 → 5 → 16 → 8 → 4 → 2 → 1 It can be seen that this sequence (starting at 13 and finishing at 1) contains 10 terms. Although it has not been proved yet (Collatz Problem), it is thought that all starting numbers finish at 1. Which starting number, under one million, produces the longest chain? NOTE: Once the chain starts the terms are allowed to go above one million. Solution(s) //int64 is required because the remainders are larger than int32 let rec Collatz (n:int64) count =     if n = 1L then         count     elif n % 2L > 0L then         Collatz ((3L*n) + 1L) (count + 1)     else         Collatz (n/2L) ...

Project Euler - Problem #13

Description Work out the first ten digits of the sum of the following one-hundred 50-digit numbers. Number is listed in solution, for brevity Solution //libraries for BitArray and BigInteger, respectively open System.Collections open System.Numerics //array of Big integer let hugeArray =     [|     37107287533902102798797998220837590246510135740250I;     46376937677490009712648124896970078050417018260538I;     74324986199524741059474233309513058123726617309629I;     91942213363574161572522430563301811072406154908250I;     23067588207539346171171980310421047513778063246676I;     89261670696623633820136378418383684178734361726757I;     28112879812849979408065481931592621691275889832738I;     44274228917432520321923589422876796487670272189318I;     47451445736001306439091167216856844588711603153276I;     ...

Project Euler - Problem #9

Description 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...

Project Euler - Problem #4

Description A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 99. Find the largest palindrome made from the product of two 3-digit numbers. Solution (Improved) The improvements are minor, but include a smaller corelation matrix amd more concise syntax. let corrMatrix start last =      seq {for i in start..last do           for j in start..last do                if i<  j then yield (i*j)}  let reverse (s:string) = new string(s |> Seq.toArray |> Array.rev) let palindromes =      (corrMatrix 100 999) |> Seq.filter (fun x -> string x = reverse(string x)) |> Seq.max Solution (Original) //function for reversing a string let reverse (s:string) = new string(s |> Seq.toArray |> Array.rev) //function to multiple each number in the series by itself, similar to a co...