Geeks With Blogs
Buhay Programmer Dont byte more than you can chew

These 2 problems have very short solutions so I'll put them together 

Problem 3:

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.

Solution:

  let prodOf3digit = 
     (100,100) |> Seq.unfold (fun (x,y) -> 
             match y with 
             | y when y < 0 -> None
             | y when x <= 999-> 
                  if (y <= 999) then
                    Some(x*y, (x,y+1))
                  else if (x+1) <= 999 then
                    Some ( (x+1)*(x+1), (x+1, x+2) )
                  else
                    None
             | y -> None )
     
           
  let palindromes = 
   prodOf3digit 
    |> Seq.filter (fun x ->
     let intStr = Int32.to_string x
     let rev (s:string) =
       let chunks =s.ToCharArray()
       let reversed = Array.rev chunks
       let r = new string(reversed)
       r
     let newNum = System.Convert.ToInt32 ( rev intStr )
      
     newNum = x 
    )
let largest =
    palindromes |> Seq.fold (fun a x -> if x > a then x else a) 0

 

The problem asked for the largest palindrome of 3-digit numbers, hence the magic number 100 and 999.  The rest of the code is pretty much self-explanatory.

Problem 6:

The sum of the squares of the first ten natural numbers is,12 + 2 + ... + 102 = 385

The square of the sum of the first ten natural numbers is, (1 + 2 + ... + 10) = 552 = 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 one hundred natural numbers and the square of the sum.

Solution:

 

let sumOfSq (n:bigint) =
   let rec getSum (x:bigint) =
     match x with
     | x when x = 1I -> 1I
     | x -> x*x + getSum (x - 1I)
    getSum n
 
 let sqOfSum n =
   let s = [ 1I .. n ] |> Seq.fold (fun a x -> a+x) 0I
   s*s
   
 let main2() =
   let diff = (sqOfSum 100I) - (sumOfSq 100I) 
   print_any diff

 

To compute the sum of squares, I've decided to use recursion,  which apart from lists and sequences, is another workhorse of functional programming.   One thing to remeber when using recursion is to always make sure you cover the base case in order to avoid looping indefinitely.  In this case it is the line x when x = 1I -> 1I

Because of the large resulting values, the type used was BigInt.

Posted on Monday, February 18, 2008 11:36 PM | Back to top


Comments on this post: F# Solution : Project Euler Problems 4 & 6

# re: F# Solution : Project Euler Problems 4 & 6
Requesting Gravatar...
Resourcefulnesses like the one you observed here will be really structural to me! I will post a connection to this page on my blog. I am certain my visitors will find that very functional.
Left by Terrell Stalberger on Nov 12, 2010 8:42 AM

Your comment:
 (will show your gravatar)


Copyright © Erik Araojo | Powered by: GeeksWithBlogs.net