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
//required library to handle very large numbers
open System.Numerics
//recursive factorial
let rec factorialTailBigInt (n:BigInteger) : BigInteger =
if n <= 1I then
1I
else
n * factorialTailBigInt (n - 1I)
//primary process
let sumFactorial =
let resFactorialBigInt = factorialTailBigInt 100I
let strCharFactorial = resFactorialBigInt.ToString()
let max = strCharFactorial.Length
let arrNum = Array.create max 0
for i=0 to max-1 do
arrNum.[i] <- System.Convert.ToInt32(strCharFactorial.Chars(i).ToString())
Array.sum arrNum
- n! means n (n 1) ... 3 2 1
- Find the sum of the digits in the number 100!
Solution
//required library to handle very large numbers
open System.Numerics
//recursive factorial
let rec factorialTailBigInt (n:BigInteger) : BigInteger =
if n <= 1I then
1I
else
n * factorialTailBigInt (n - 1I)
//primary process
let sumFactorial =
let resFactorialBigInt = factorialTailBigInt 100I
let strCharFactorial = resFactorialBigInt.ToString()
let max = strCharFactorial.Length
let arrNum = Array.create max 0
for i=0 to max-1 do
arrNum.[i] <- System.Convert.ToInt32(strCharFactorial.Chars(i).ToString())
Array.sum arrNum
Comments
Post a Comment