May 2008
12 posts
FSharp Euler #9
The problem statement: A Pythagorean triplet is a set of three natural numbers, a a2 + b2 = c2 For example, 32 + 42 = 9 + 16 = 25 = 52. There exists exactly one Pythagorean triplet for which a + b + c = 1000. Find the product abc. This is another one that’s probably really more “math”-y than programming-y, but I’m enjoying using my new F# hammer to solve these, so...
Euler #8 in FSharp
The problem: Find the greatest product of five consecutive digits in the 1000-digit number. 73167176531330624919225119674426574742355349194934 96983520312774506326239578318016984801869478851843 85861560789112949495459501737958331952853208805511 12540698747158523863050715693290963295227443043557 66896648950445244523161731856403098711121722383113 ...
Project Euler, problem #7 in FSharp
The problem is: By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13. What is the 10001st prime number? Well, this sort of looks like another one that’s intended more for pencil-and-paper solvers than programming-language solvers. But, maybe prime #10001 is very big, so it becomes a problem for code too. My first thought is just to loop with...
Euler Project in F#, problem #6
Kick it, Euler: The sum of the squares of the first ten natural numbers is,1² + 2² + … + 10² = 385 The square of the sum of the first ten natural numbers is,(1 + 2 + … + 10)² = 55² = 3025 Hence the difference between the sum of the squares of the first ten natural numbers and the square of the sum is 3025 − 385 = 2640. Find the difference between the sum of the squares of the first...
Euler is streaking in F#, problem #5
Problem #5: 2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder. What is the smallest number that is evenly divisible by all of the numbers from 1 to 20? Speaking of infinite sequences, here’s the solution that seemed obvious to me at first.open Microsoft.FSharp.Math Seq.init_infinite (fun x -> BigInt.FromInt32 x) |>...
…
Because we couldn’t see how the system worked anymore!
Small...
– http://steve-yegge.blogspot.com/2008/05/dynamic-languages-strike-back.html
Some more Euler Project, problem #4
This one was pretty straightforward. A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 × 99. Find the largest palindrome made from the product of two 3-digit numbers. The hardest part was just reversing the number to be able to tell if it was a palindrome. Unfortunately, when I searched for “reverse string...
More F# thoughts
It’s pretty irritating so far, but I can’t tell if that’s because I don’t know what I’m doing or because I don’t have some key insight into the language structure yet, or what. I find I’m poking around a lot trying to figure out if I need a terminator or a keyword in random places, and because of the structure of the language there’s not as many...
Euler #3
The problem statement: The prime factors of 13195 are 5, 7, 13 and 29. What is the largest prime factor of the number 600851475143 ? The first thing we need to do likely is know if a number is prime:let isprime x = x > 1 && not(Seq.exists (fun n -> x % n = 0) {2..int(sqrt (float x))}) This is a little sneaky. First, it generates a list of numbers from 2 to the square...
Euler Problem #2, redux
This is a similar attempt, not sure if it’s better or worse really, but uses comprehensions instead.{ for i in [1..33] do let x = fib(i) if x < 4000000 && x % 2 = 0 then yield x } |> Seq.sumByInt(fun x -> x)
Euler Problem #2
(only 191 problems to go, currently) Problem #2 was stated as: Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be: 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, … Find the sum of all the even-valued terms in the sequence which do not exceed four million. Pretty straightforward I guess.let rec fib n = if n ...
F# and Project Euler
I was vaguely looking for something that might make me smarter, wouldn’t be at all practical, and also wouldn’t be a huge time investment. I started by working through the exercises of “Introduction to Algorithms” by C/L/R, but I didn’t make it too far. Maybe someday. Project Euler is some math/algorithmic problems that seem to fit the bill. Since I was thinking I...