### Project Euler - Problem 12

**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