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 sequence of triangle numbers is generated by adding the natural numbers. So the 7th triangle number would be 1 + 2 + 3 + 4 + 5 + 6 + 7 = 28. The first ten terms would be:
1, 3, 6, 10, 15, 21, 28, 36, 45, 55, ...
Let us list the factors of the first seven triangle numbers:
1: 1
3: 1,3
6: 1,2,3,6
10: 1,2,5,10
15: 1,3,5,15
21: 1,3,7,21
28: 1,2,4,7,14,28
We can see that 28 is the first triangle number to have over five divisors.
What is the value of the first triangle number to have over five hundred (500) divisors?
Solution
//from rosetta code site
//code for finding the distinct factors of a number
let factors number = seq {
for divisor in 1 .. (float >> sqrt >> int) number do
if number % divisor = 0 then
yield divisor
yield number / divisor
}
//verification of factor code
let factorTest = Seq.toArray(factors 28)
//basic method for calculating the triangle number oin a recursive function
let rec generateTriangleNumber (priorValue:int) (position:int) =
priorValue + position
//recursion for finding triangle numbers unique factors
let rec FindTriangleNumberDivisibleBy priorValue position divisorLimit=
let newNum = generateTriangleNumber priorValue position
let result = factors newNum |> Seq.distinct |> Seq.toArray
if result.Length > divisorLimit then
newNum
else
FindTriangleNumberDivisibleBy newNum (position + 1) divisorLimit
let findIt = FindTriangleNumberDivisibleBy 0 1 500
Comments
Post a Comment