Project Euler for F#un

Learning F# through Project Euler

Sunday, November 21, 2010

Problem 50: Which prime, below one-million, can be written as the sum of the most consecutive primes?

›
Alright, we completed through problem 50! First, I thought it was about time I implemented a decent prime sequence generator instead of co...
Friday, November 19, 2010

Problem 49: What 12-digit number do you form by concatenating the three terms in this sequence?

›
This was a tough problem. Even after dreaming up an algorithm which would perform well enough, it went through several tweaks until I was sa...
Saturday, November 13, 2010

Problem 48: Find the last ten digits of the series, 1^1 + 2^2 + 3^3 + ... + 1000^1000.

›
Again, easy problem using functional composition, the “hard” work was done in our previous implementation of the Digits module. The answer ...

Problem 47: Find the first four consecutive integers to have four distinct primes factors. What is the first of these numbers?

›
Not the best performing solution (perhaps inherently so, though), but a shining example of the power of functional composition (the hard wor...
Friday, November 12, 2010

Problem 46: What is the smallest odd composite that cannot be written as the sum of a prime and twice a square?

›
The difficult part of this problem was working around F#'s lack of break, continue, and return in for loops. hasForm would be most easi...
Sunday, July 18, 2010

Problem 45: After 40755, what is the next triangle number that is also pentagonal and hexagonal?

›
Here we reused the technique from Problem 44 for quickly asserting that a number is in the range of a function by verifying that n = f (f-1(...
Saturday, July 17, 2010

Problem 44: Find the pair of pentagonal numbers, Pj and Pk, for which their sum and difference is pentagonal and D = |Pk - Pj| is minimised; what is the value of D?

›
This problem was a big disappointment.  It turns out that the first pair you find for which their sum and difference is pentagonal is the so...
Sunday, July 4, 2010

Problem 43: Find the sum of all 0 to 9 pandigital numbers with the given property.

›
Combining our Digits and permutations implementations with F#’s built-in Seq functions, we really see how functional programming shines i...
Tuesday, June 29, 2010

Problem 42: How many triangle words does the list of common English words contain?

›
This problem is designed to make use of several tools. let problem42a = let triangle n = let n' = n |> float in (0.5)*(n')*...
Tuesday, June 22, 2010

Problem 41: What is the largest n-digit pandigital prime that exists?

›
My first approach was to try and find the first odd number descending from 987654321 which was both prime and pandigital, but despite attemp...
Sunday, June 20, 2010

Problem 40: If dn represents the nth digit of the fractional part, find the value of the following expression. d1 * d10 * d100 * d1000 * d10000 * d100000 * d1000000

›
An imperative solution seems appropriate. let problem40a = let sb = System.Text.StringBuilder() let mutable n = 1 while sb.Len...
Friday, June 18, 2010

Problem 39: For which value of p ≤ 1000, is the number of solutions maximised?

›
Brute force algorithm with no real optimizations. let problem39c = let countTriplets p = let mutable count = 0 for a ...
Thursday, June 17, 2010

Problem 38: What is the largest 1 to 9 pandigital that can be formed by multiplying a fixed number by 1, 2, 3, ... ?

›
Wow, interesting - tough!  And I finally found Seq.choose (I was looking for Seq.pickAll). let problem38a = let isPosPd (nstr:string)...
Wednesday, June 16, 2010

Problem 37: Find the sum of all eleven primes that are both truncatable from left to right and right to left.

›
Kind of a variation on Problem 35.  I was surprised by how fast it ran! let problem37a = let isTruncatablePrime n = if n |>...
Monday, June 14, 2010

Problem 36: Find the sum of all numbers less than one million, which are palindromic in base 10 and base 2.

›
Reusing isPalindrome from Problem 4 and leveraging System.Convert for obtaining the binary representation of a given decimal number, this pr...

Problem 35: How many circular primes are there below one million?

›
Now we get to use our Digits module.  The most interesting part of this algorithm is performing digit rotations (the assignment of r ), whic...

Digits

›
We’ve had a lot of problems involving digit manipulation and so far have implemented ad-hoc string parsing solutions.  But this approach is ...
Saturday, June 12, 2010

Problem 34: Find the sum of all numbers which are equal to the sum of the factorial of their digits.

›
The upper bound is 2540160, since (factorial 9)*7 = 2540160 and (factorial 9)^x grows faster than 10^x for x >= 7. let problem34b = ...
Friday, June 11, 2010

Problem 33: Discover all the fractions with an unorthodox cancelling method.

›
With F#, there are two ways to solve this: the easy way, and the slightly easier way.  The first way, we’ll work entirely with integers, inc...
Friday, June 4, 2010

Problem 32: Find the sum of all products whose multiplicand/multiplier/product identity can be written as a 1 through 9 pandigital.

›
While having lots of pieces, this problem is mostly straightforward.  The only part that required any analysis is determining sufficient low...
Wednesday, June 2, 2010

Problem 31: How many different ways can £2 be made using any number of coins?

›
More fun with recursive sequence expressions! In this solution, we generate all of the combinations instead of only counting them. let pro...
Sunday, May 30, 2010

Problem 30: Find the sum of all the numbers that can be written as the sum of fifth powers of their digits.

›
We must find an upper limit for numbers to search.  As the length of a number increases, 9**5 is the greatest additional contribution a di...
Saturday, May 29, 2010

Problem 29: How many distinct terms are in the sequence generated by a^(b) for 2 ≤ a ≤ 100 and 2 ≤ b ≤ 100?

›
Too easy. let problem29a = seq { for a in 2I..100I do for b in 2..100 do yield bigint.Pow(a,b)} |> Seq.distinct |> Seq....
1 comment:

Problem 28: What is the sum of the numbers on the diagonals in a 1001 by 1001 number spiral?

›
Another pleasant one where by writing down the first few terms of the sequence (by dimension) of sequences (corners in a dimension), it was ...
Friday, May 28, 2010

Problem 27: Find the product of the coefficients, a and b, for the quadratic expression that produces the maximum number of primes for consecutive values of n, starting with n = 0.

›
This one was straight forward, just had to follow instructions (I didn’t apply any mathematical optimizations, because it looked like if onc...
Thursday, May 27, 2010

Problem 26: Find the value of d < 1000 for which 1/d contains the longest recurring cycle in its decimal fraction part.

›
After taking a break for a few days, having spent 3 consecutive weeks solving problems 1-25 every free hour and deep into the night, this tu...

Parallelization with PLINQ

›
The past couple of days I've been interested in exploring PLINQ with F#.  But Since I’m using F# 2.0 for Visual Studio 2008 (i.e. .NET 2...
Saturday, May 22, 2010

Problem 25: What is the first term in the Fibonacci sequence to contain 1000 digits?

›
The key to this one, unlike the traditional non-tail-recursive definition of the Fibonacci sequence, is to generate the next element in the ...
Friday, May 21, 2010

Problem 24: What is the millionth lexicographic permutation of the digits 0, 1, 2, 3, 4, 5, 6, 7, 8 and 9?

›
Completing this one marks my advance to “level 1”, having completed 25 problems (I completed Problem 67 when I completed Problem 18), and pl...
Thursday, May 20, 2010

Problem 23: Find the sum of all the positive integers which cannot be written as the sum of two abundant numbers.

›
Since we’ll be reusing the sigma-divisor function again, we’ll generalize it and move it to Calc.Generalize.  Along with that I have added a...
Wednesday, May 19, 2010

Problem 22: What is the total of all the name “scores” in the file?

›
Due to the nature of this problem, we don’t shy away from mutation for performance gains.  For example, sorting the names array in place.  T...
Tuesday, May 18, 2010

Problem 21: Evaluate the sum of all the amicable numbers under 10000.

›
First, using our fast factorize function, we implement the well-known sigma-divisor function and it’s proper companion. let sigma n = ...
Saturday, May 15, 2010

LanguagePrimitives and Generic Math

›
Over the past few days I’ve been exploring ways to organize and reuse common functions which repeat across Euler problems.  Something I’ve b...

Problem 20: Find the sum of digits in 100!

›
Reusing the fact function we slipped into Problem 15, this is essentially the same as Problem 16. let problem20a = fact 100I |> st...

Problem 19: How many Sundays fell on the first of the month during the twentieth century?

›
Relieved .NET DateTime was sufficient for this. let problem19a = let mutable cur = new System.DateTime(1901, 1, 1); let last = ne...

Problem 18: Find the maximum total from top to bottom of the triangle below.

›
Wow.  The algorithm just took a little bit of pacing to realize, but I’ve gone through several refinements and variations of the implementat...
Friday, May 14, 2010

Problem 17: If all the numbers from 1 to 1000 (one thousand) inclusive were written out in words, how many letters would be used?

›
After simplifying, and taking care to spell “forty” correctly. let problem17d = "OneThousand" .Length + "OneHu...
Thursday, May 13, 2010

Problem 16: What is the sum of the digits of the number 2**1000?

›
Easy. let problem16a = bigint.Pow(2I, 1000) |> string |> Seq.sumBy ( fun c -> c |> string |> int)

Problem 15: Starting in the top left corner, how many routes (without backtracking) are there through a 20 by 20 grid to the bottom right corner?

›
OK.  I am incredibly pleased with myself for solving this one almost entirely through mathematical proof.  Indeed, I’ve solved it generally ...

Problem 14: Which starting number, under one million, produces the longest “Collatz” chain?

›
I coded  this solution in just a couple of minutes, but ran into trouble with F#’s unchecked Int32 operations.  We can do checked operations...
Wednesday, May 12, 2010

Problem 13: Work out the first ten digits of the sum of the following one-hundred 50-digit numbers.

›
With F#’s bigint, this is trivial. let problem13a = [37107287533902102798797998220837590246510135740250I; 46376937677490009712...
1 comment:

Problem 12: What is the value of the first triangle number to have over five hundred divisors?

›
This has been the most sophisticated problem yet. My first solution took 2 minutes to write, but I gave up on it after 5 minutes of runnin...
2 comments:
Monday, May 10, 2010

Problem 11: What is the greatest product of four numbers on the same straight line in the 20 by 20 grid?

›
Not too hard, but it offered some frustration.  First, I couldn’t get the compiler to parse my multi-line list of lists for the grid.  Secon...

Problem 10: Find the sum of all the primes below two million.

›
This problem is a variation on Problem 7 allowing us to reuse isPrime and modify nthPrime to build a list of primes up to a max. //problem...

Problem 9: There exists exactly one Pythagorean triplet for which a + b + c = 1000. Find the product abc.

›
Here we generate a sequence of all possible triplets meeting the conditions, and find the first which is Pythagorean.  It took a while to ge...
›
Home
View web version

About Me

Stephen Swensen
I'm developing Unquote, a library which allows you to write F# unit test assertions as quoted expressions and get step-by-step failure messages for free

I'm developing FsEye, a visual object tree inspector for the F# Interactive

I'm working through Project Euler with F#

I contribute to The Code Project
View my complete profile
Powered by Blogger.