Solving Project Euler problems as a way to learn F#.

Click here for additional resources.

Solving Project Euler problems as a way to learn F#.

Click here for additional resources.

Showing posts from July, 2012

- Get link
- Other Apps

- Get link
- Other Apps

Problem If the numbers 1 to 5 are written out in words: one, two, three, four, five, then there are 3 + 3 + 5 + 4 + 4 = 19 letters used in total. If all the numbers from 1 to 1000 (one thousand) inclusive were written out in words, how many letters would be used? NOTE: Do not count spaces or hyphens. For example, 342 (three hundred and forty-two) contains 23 letters and 115 (one hundred and fifteen) contains 20 letters. The use of "and" when writing out numbers is in compliance with British usage. Note I have done two (2) solutions for this problem. The original in a prior post requires less code, but is 'ugly' and is less reusable that this match-based solution. This solution is based on smaller methods using the match function, with larger number functions able to call and reuse code from the functions for smaller numbers. Calling the method is much cleaner as well. Solution ( Match-Based ) let GetThousandthsLetters prefix fourth third second first =

- Get link
- Other Apps

Problem If the numbers 1 to 5 are written out in words: one, two, three, four, five, then there are 3 + 3 + 5 + 4 + 4 = 19 letters used in total. If all the numbers from 1 to 1000 (one thousand) inclusive were written out in words, how many letters would be used? NOTE: Do not count spaces or hyphens. For example, 342 (three hundred and forty-two) contains 23 letters and 115 (one hundred and fifteen) contains 20 letters. The use of "and" when writing out numbers is in compliance with British usage. Solution //this is a very ugly solution, which I will return to to properly factor to smaller recursive methods let GetThousandsLetters num = match num with | 1 -> "one thousand" | 2 -> "two thousand" | 3 -> "three thousand" | 4 -> "four thousand" | 5 -> "five thousand" | 6 -> "six thousand" | 7 -> "seven thousand&qu

- Get link
- Other Apps

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 gene

- Get link
- Other Apps

Description Starting with the number 1 and moving to the right in a clockwise direction a 5 by 5 spiral is formed as follows: 21 22 23 24 25 20 7 8 9 10 19 6 1 2 11 18 5 4 3 12 17 16 15 14 13 It can be verified that the sum of the numbers on the diagonals is 101. What is the sum of the numbers on the diagonals in a 1001 by 1001 spiral formed in the same way? Solution //calculates the sum for the level of the square //each increase in level only requires calculating 4 new values, the corners let levelValue (start:int) (side:int) = let diffAtLevel = side - 1 let sum = (start + diffAtLevel) + (start + (diffAtLevel * 2)) + (start + (diffAtLevel * 3)) + (start + (diffAtLevel * 4)) sum //recursively iterates from level 0, which is the center //to the outer part of the array //and for each level calculates the additional sum and sums that to running sum let rec sumByPowers start priorLevelSquared level endSide sum = if level > endSide

- Get link
- Other Apps